Adding new item to exist data

48 views Asked by At

I try to use JavaScript to write a recursion function that a new items (array of of objects) to a exist data (unknown size json).

Need some help with that.

The exist data: The data is contain nodes and children.Each children could be more children or node (leaf)

var data = [
    {
        key:"root",
        children:[
            {
                key:"a0",
                children:[]
            },
            {
                key:"a1",
                children:[
                    {
                        key:"a10",
                        children:[]
                    },
                    {
                        key:"a11",
                        children:[]
                    },
                    {
                        key:"a12",
                        children:[]
                    }
                ],
            },
            {
                key:"a1",
                children:[]
            }
        ]
    }
]

New items I want to add to the exist data:

var result = [
    {key:"a1"},
    {key:"a12"},
    {key:"21"}
]

At the end of adding the new items the data should be like that: The result could be different each time , but always be a simple array of object.

var data = [
    {
        key:"root",
        children:[
            {
                key:"a0",
                children:[]
            },
            {
                key:"a1",
                children:[
                    {
                        key:"a10",
                        children:[]
                    },
                    {
                        key:"a11",
                        children:[]
                    },
                    {
                        key:"a12",
                        children:[
                            {key:"21"}
                        ]
                    }
                ],
            },
            {
                key:"a1",
                children:[]
            }
        ]
    }
]

thanks

2

There are 2 answers

0
Nenad Vracar On

You can do this with reduce method and pass initial array as accumulator.

var data = [{"key":"root","children":[{"key":"a0","children":[]},{"key":"a1","children":[{"key":"a10","children":[]},{"key":"a11","children":[]},{"key":"a12","children":[]}]},{"key":"a1","children":[]}]}]

var result = [
  {key:"a1"},
  {key:"a12"},
  {key:"21"}
]

result.reduce((r, e, i, arr) => {
  let obj = r && r.find(({key}) => key == e.key);
  if (obj && arr[i + 1]) return obj.children
  else if (r && !arr[i + 1]) r.push(e)
  else return null
}, data[0].children)

console.log(data)

0
Anurag Singh Bisht On

You can use recursion to solve the problem. I have followed the following steps

  • addChildren(item, result) - This function will receive the data and result and will return the updated object.
  • First check if result is empty or not.
  • If result is not empty, get the first element of result by using Array.shift
  • Check if the key of the object exist in the array.
  • If object exists, call the addChildren function again with the new parameters
  • If object does not exists, create the object and return the array.

var data = [
    {
        key:"root",
        children:[
            {
                key:"a0",
                children:[]
            },
            {
                key:"a1",
                children:[
                    {
                        key:"a10",
                        children:[]
                    },
                    {
                        key:"a11",
                        children:[]
                    },
                    {
                        key:"a12",
                        children:[]
                    }
                ],
            },
            {
                key:"a2",
                children:[]
            }
        ]
    }
];

var result = [
    {key:"root"},
    {key:"a1"},
    {key:"a12"},
    {key:"21"}
]

function addChildren(item, result) {
  if (!result.length) { return []; }
  
  var res = result.shift();
  
  if (item.some(i => i.key === res.key)) {
    return item.map(i => {
      if (i.key === res.key) {
        return {
          key: i.key,
          children: addChildren(i.children, result)
        };
      }
      return i;
    });
  } else {
    item.push({
      key: res.key,
      children: addChildren([], result)
    });
  }
  return item;
}

console.log(addChildren(data, result));