I want to filter an array like this:
const myArray = [
{ ageIndex: 4, nameIndex: 1, type: "group" },
{ ageIndex: 4, nameIndex: 0, type: "group" },
{ ageIndex: 5, nameIndex: 0, type: "person" },
{ ageIndex: 5, nameIndex: 1, type: "person" },
{ ageIndex: 5, type: "group" },
];
The new array should filter by unique ageIndex, but only when the type is group
. The other objects of type person
should stay unchanged. The nameIndex
doesn't matter. There are group objects without a nameIndex.
const myNewArray = [
{ ageIndex: 4, nameIndex: 1, type: "group" },
{ ageIndex: 5, nameIndex: 0, type: "person" },
{ ageIndex: 5, nameIndex: 1, type: "person" },
{ ageIndex: 5, type: "group" },
];
How can I filter it like that?
question from:
https://stackoverflow.com/questions/65898433/filter-array-of-objects-with-conditions 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…