I have object key value in below format.
{
"Code1": {
"char10": "ch1",
"number1": "1",
"text1": "txt1"
},
"Code2": {
"char2": "ch2",
"num2": "2"
},
"Code3": {
"text": "txt4"
}
}
Would like to convert to this format :
{
"Code1": [
{
"char10": "ch1",
"number1": "1",
"text1": "txt1"
}
],
"Code2": [
{
"char2": "ch2",
"num2": "2"
}
],
"Code3": [
{
"text": "txt4"
}
]
}
Managed to achieve to get somewhat similar response but not exact output which I am looking for.
Tried the below snippet but it returns diff format than expected.
Object.entries(payload).map((e) => ( { [e[0]]: e[1] } ))
Response with above snippet :
[
{
"Code1": {
"char10": "ch1",
"number1": "1",
"text1": "txt1"
}
},
{
"Code2": {
"char2": "ch2",
"num2": "2"
}
},
{
"Code3": {
"text": "txt4"
}
}
]
You could get all entries and map with wrapped values for a new object.
const
data = { Code1: { char10: "ch1", number1: "1", text1: "txt1" }, Code2: { char2: "ch2", num2: "2" }, Code3: { text: "txt4" } },
result = Object.fromEntries(Object
.entries(data)
.map(([k, v]) => [k, [v]])
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }