I am faced with a challenge where I need to eliminate array members that do not have a certain property. Take for example this scenario:
const idToKeep = 1;
const objList = [{
id: 1,
name: 'aaa',
},
{
id: 4,
name: 'bbb',
},
{
id: 7,
name: 'ccc',
},
];
In this case, I want to remove all objects that do not have an id of 1. While I understand how to remove items from the list, my question is how to retain and remove only those that don't meet the criteria.
To achieve this, I can implement the following code snippet:
const filteredObjList = objList.filter(x => x.id === idToStay);
console.log(filteredObjList);