如何将值与嵌套数组中的其他对象进行比较?

How to compare value with other objects in nested array?

如何将嵌套数组值与父对象中的对象进行比较,如果不匹配则删除该对象?我需要遍历整个数组,只在与父 "module".

匹配的子项中留下 "modules"

Here is the image of the console to see my array structure.

for (var i = topicArray.length - 1; i >= 0; i--) {
if (topicArray[i].module !== module)
topicArray.splice(i, 1) }

一个选择是遍历每个项目,然后简单地 filter() 排除不匹配的模块:

const data = [
  {
    module: 'A',
    topics: [
      { topic: 'something', module: 'A' },
      { topic: 'something else', module: 'B' }
    ]
  },
  {
    module: 'B',
    topics: [
      { topic: 'something', module: 'A' },
      { topic: 'something else', module: 'B' }
    ]
  }
]
    
data.forEach(item => {
  item.topics = item.topics.filter(topic => item.module === topic.module)
})

console.log(data)