Boss, take a look at the logic problem.

var a = [
                {
                    name: "a",
                    child: [{
                            name: "bb",
                            limit: 12
                        },
                        {
                            name: "ss",
                            limit: 12
                        }
                    ]
                }, 
                {
                    name: "b",
                    child: [{
                            name: "ss",
                            limit: 12
                        },
                        {
                            name: "kk",
                            limit: 12
                        }
                    ]
                }, 
                {
                    name: "c",
                    child: [{
                            name: "aa",
                            limit: 12
                        },
                        {
                            name: "mm",
                            limit: 12
                        }
                    ]
                }, 
                {
                    name: "d",
                    child: [{
                            name: "aa",
                            limit: 12
                        },
                        {
                            name: "mm",
                            limit: 12
                        }
                    ]
                }
            ];
            var b = [
                {
                    name: "b",
                    child: [{
                        name: "ss"
                    },{
                        name:"kk"
                    }]
                }, 
                {
                    name: "a",
                    child: [{
                        name: "ss"
                    }]
                }
            ];
            var c = [];
            a.forEach(function(e) {
                b.forEach(function(i) {
                    if(e.name == i.name) {
                        e.child.forEach(function(j, index1) {
                            i.child.forEach(function(k, index2) {
                                if(j.name !== k.name) {
                                    e.child.splice(index1,1);
                                    console.log(e.child)
                                }
                            })
                        })
                        c.push(e);
                    };
                })
        });
            console.log(c)

I want to find out all the things in a that are the same as b name, and why do I find out that they are like this

clipboard.png

Mar.17,2021

can you take one more sentence to explain what's wrong with you?


you sort it out. Is there a problem in logic? What is the required output information?
the code you provided seems to be incomplete, and the objects in the later program implementation, such as a and b , do not know where they came from.


imagine what e.child.forEach would look like to traverse to the last element.


clipboard.png


var c = a.filter(e => {
  let index = b.findIndex(f => e.name === f.name)
  if (index === -1) return false
  let child2 = b[index].child
  e.child = e.child.filter(g => {
    let index = child2.findIndex(h => g.name === h.name)
    return index !== -1
  })
  return true
})
Menu