我尝试使用下面的技术,但没有得到想要的结果,有人愿意分享他们的知识吗?将不胜感激:)
for (let i = 0; i < categories.length; i++) {
for (let j = 0; j < categories[i].features.length - 1; j++) {
if (categories[i].features[j] !== categories[i].features[j+ 1]) {
newCataegories.push(categories[i].features[j]);
console.log(categories[i].features[j].id, j);
}
}
}
categories.forEach((element, index) => {
element.features.forEach((e, i) => {
if (newCataegories.indexOf(e) === -1) {
newCataegories.push(e);
}
});
});
答案 0 :(得分:0)
您可以像这样使用 Array.filter
和 Array.includes
:
const array1 = ["a", "b", "c", "d"]
const array2 = ["a", "c"]
// Filter the array by making sure the item isn't in array 2
const array3 = array1.filter((item) => !array2.includes(item))
console.log(array3) // ["b", "d"]
编辑:const 声明(facepalm)
答案 1 :(得分:0)
您可以将值放入集合中。事实上,您可以使用 Set
构造函数回调来为它提供所有特征值,使用 flatMap
。然后只剩下将该集合转换为数组(如果这是您需要的):
let result = Array.from(new Set(categories.flatMap(({features}) => features)));