我必须“过滤”数组中对象的 ID,我找到了这种方法,但我认为这不是最好的方法:
const idToCompare = 456
MyArray = [{name: 'One, brand: [{id: 456, name:'Hello'},{id: 857, name:'Hi'},{id: 456, name:'Goodbye'},{id: 123, name:'See you'}]} ]
所以现在我这样做:
let filtered = myArray.map(a => a.brand.filter(b => b.id === idToCompare))
Console.log(filtered ) // [[{id: 456, name:'Hello'},{id: 456, name:'Goodbye'}]]
我需要 flat() 它有
filtered.flat()
Console.log(filtered ) // [{id: 456, name:'Hello'},{id: 456, name:'Goodbye'}]
我怎样才能以最好的方式或简单的方式做?
aluckdog
相关分类