1

I have recursive array of object collection which need to convert to object,

I tried something like below but I want dynamically.

Can anyone give suggestions or share your ideas it will helpful for me

let arr = list;
const jsonObj = {};
const arrayToObject = (array, keyField) =>
array.reduce((obj, item) => {
  obj[item[keyField]] = item
  return obj
}, {})
arr.forEach((item) => {
  jsonObj[item.key] = {};
  if(item.children.length > 0) {
     jsonObj[item.key] =  arrayToObject(item.children, 'key');
  }
})

INPUT

list =  [
    {
        key: "Parent 1",
        value: "",
        children: [
            { key: 11, value: "Child 1", children: [] },
            { key: 12, value: "Child 2", children: [] }
        ]
    },
    {
        key: "Parent 2",
        value: "",
        children: [
            {
                key: 20,
                value: "",
                children: [
                    { key: 21, value: "Grand Child 1", children: [] },
                    { key: 22, value: "Grand Child 2", children: [] }
                ]
            }
        ]
    },
    {
        key: "Parent 3",
        value: "",
        children: [
            { key: 31, value: "Child 1", children: [] },
            { key: 32, value: "Child 2", children: [] }
        ]
    },
    ];

OUTPUT

{
    "Parent 1": {
        "11": "Child 1",
        "12": "Child 2",
    },
    "Parent 2": {
        "20": {
            "21": "Grand Child 1",
            "22": "Grand Child 2",          
        }
    },
    "Parent 3": {
        "31": "Child 1",
        "32": "Child 2",
    }
}

1 Answer 1

1

You could use reduce to recursively loop the array. If the current object has a non-zero children array, call transform recursively. Else, use the value for key in the accumulator

const list = [{key:"Parent 1",value:"",children:[{key:11,value:"Child 1",children:[]},{key:12,value:"Child 2",children:[]}]},{key:"Parent 2",value:"",children:[{key:20,value:"",children:[{key:21,value:"Grand Child 1",children:[]},{key:22,value:"Grand Child 2",children:[]}]}]},{key:"Parent 3",value:"",children:[{key:31,value:"Child 1",children:[]},{key:32,value:"Child 2",children:[]}]}];

function transform(array) {
  return array.reduce((r, { key, value, children }) => {
    if (children.length)
      r[key] = transform(children)
    else
      r[key] = value;
    return r;
  }, {})
}

console.log(transform(list))

One-liner using arrow function and Object.assgin():

const transform = (array) => array.reduce((r, { key, value, children }) => 
    Object.assign(r, { [key]: children.length ? transform(children): value }), {})
Sign up to request clarification or add additional context in comments.

1 Comment

i need vice versa output, nested object to recursive array any suggestions or ideas

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.