var listToDelete = ['abc', 'efg']; var arrayOfObjects = [{id:'abc',name:'oh'}, // delete me {id:'efg',name:'em'}, // delete me {id:'hij',name:'ge'}] // all that should remain
如何通过匹配对象属性从数组中删除对象?
请只使用本机JavaScript。
我在使用接头时遇到麻烦,因为每次删除的长度都会减少。使用克隆并在原始索引上进行拼接仍然会给您带来长度减少的问题。
我以为你用过splice这样的东西?
splice
for (var i = 0; i < arrayOfObjects.length; i++) { var obj = arrayOfObjects[i]; if (listToDelete.indexOf(obj.id) !== -1) { arrayOfObjects.splice(i, 1); } }
修复bug所需要做的就是i在下一次减少,然后(也可以选择向后循环):
i
for (var i = 0; i < arrayOfObjects.length; i++) { var obj = arrayOfObjects[i]; if (listToDelete.indexOf(obj.id) !== -1) { arrayOfObjects.splice(i, 1); **i--;** } }
为了避免线性时间删除,可以编写要 保留 在数组上的数组元素:
var end = 0; for (var i = 0; i < arrayOfObjects.length; i++) { var obj = arrayOfObjects[i]; if (listToDelete.indexOf(obj.id) === -1) { arrayOfObjects[end++] = obj; } } arrayOfObjects.length = end;
为了避免在现代运行时中进行线性时间查找,可以使用哈希集:
const setToDelete = new Set(listToDelete); let end = 0; for (let i = 0; i < arrayOfObjects.length; i++) { const obj = arrayOfObjects[i]; if (setToDelete.has(obj.id)) { arrayOfObjects[end++] = obj; } } arrayOfObjects.length = end;
可以包装成一个不错的功能:
const filterInPlace = (array, predicate) => { let end = 0; for (let i = 0; i < array.length; i++) { const obj = array[i]; if (predicate(obj)) { array[end++] = obj; } } array.length = end; }; const toDelete = new Set(['abc', 'efg']); const arrayOfObjects = [{id: 'abc', name: 'oh'}, {id: 'efg', name: 'em'}, {id: 'hij', name: 'ge'}]; filterInPlace(arrayOfObjects, obj => !toDelete.has(obj.id)); console.log(arrayOfObjects);
如果您不需要就位,那就是Array#filter:
Array#filter
const toDelete = new Set(['abc', 'efg']); const newArray = arrayOfObjects.filter(obj => !toDelete.has(obj.id));