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"
}
}
]
Array.map()will always return an arrayfromEntriesto take the mapped output ofentriesand create an object from it. You were off with yourmapcall, that's all.for (let key in payload) payload[key] = [payload[key]];is a basic way. Or assign the props to a new object.