0

I have a source array and target array, based on the target array need to update the source array

sourceAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]},
{name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'}]},
];

targetAry = [{name:'Label1', value: 'label1', children:[{name:'Anu'}]},
{name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'}]},
];

resultAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'}]}
]},
];

Code which I try

let resultAry = sourceAry.map((obj) => {
      obj.children.map((elem) =>{
        targetAry.filter(parent => parent.children.filter((el) => {
          el.name !== elem.name}))
      })
    })
    console.log(resultAry, 'NEW', list);
2
  • so you need to remove children in targetAry from corresponding children in sourceAry ? Commented Aug 13, 2020 at 14:09
  • yes, i need to remove if all the child present in target Commented Aug 13, 2020 at 14:10

2 Answers 2

1

you could start start with some facilities to make it simpler:

const indexBy = (f, data) => data.reduce((acc, x) => Object.assign(acc, { [f(x)]: x }), {})

const remove = (keyFn, dataToRemove, from) => {
  const dataToRemoveIndexed = indexBy(keyFn, dataToRemove);
  return from.filter(it => !(keyFn(it) in dataToRemoveIndexed));
}

we introduce the indexBy here, to make removal O(m+n), instead of O(m^2) (if there are many items in the collection to check)

then you can use it like this:

const targetIndexed = indexBy(it => it.name, targetAry);

const result = sourceAry.map(
  it => ({ 
    ...it, 
    children: remove(
      it => it.name, 
      (targetIndexed[it.name] || {}).children || [],
      it.children
    )
  })
)

so it leaves you with the following result:

[
 {"name":"Label1","value":"label1","children":[{"name":"Ammu"}, {"name":"Rahual"}]},
 {"name":"Label2","value":"label2","children":[]}
]

if you also want to delete the item with empty children, you can just filter it out: result.filter(it => it.children.length > 0)

Sign up to request clarification or add additional context in comments.

Comments

1

Ciao, try something like this:

sourceAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]}, {name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'},{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]}, {name:'Label3', value: 'label3', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]} ];

targetAry = [{name:'Label1', value: 'label1', children:[{name:'Anu'}]},
{name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'}]},
];

let result = [];
sourceAry.forEach(source => {
       let filter = targetAry.filter(target => target.name === source.name)
       if (filter.length > 0) {      
           let filterchildren = source.children.filter(a => !filter[0].children.map(b=>b.name).includes(a.name));
           if (filterchildren.length > 0) {
              let resultobj = source;
              resultobj.children = filterchildren;
              result.push(resultobj);
           }
       }
       else result.push(source);
})

console.log(result)

I filter targetAry based on sourceAry name. Then subtract children with .filter(a => !filter[0].children.map(b=>b.name).includes(a.name)); and finally push element found in result array.

2 Comments

It is wrong, when the sourceAry have this value sourceAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]}, {name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'},{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]}, {name:'Label3', value: 'label3', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]} ];
Ah because if there is no element in targetAry that maches with one in sourceAry you want to return all the element. Ok, I updated my answer.

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.