按对象属性从数组中删除对象

时间:2013-05-10 22:36:20

标签: javascript

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。

我在使用拼接时遇到问题,因为每次删除都会缩短长度。 在orignal索引上使用克隆和拼接仍然会让你遇到缩短长度的问题。

14 个答案:

答案 0 :(得分:134)

我假设你使用了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,然后(向后循环也是一个选项):

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

const toDelete = new Set(['abc', 'efg']);
const newArray = arrayOfObjects.filter(obj => !toDelete.has(obj.id));

答案 1 :(得分:61)

您可以通过其中一个属性删除项目,而无需使用任何第三方库,如下所示:

var removeIndex = array.map(function(item) { return item.id; })
                       .indexOf("abc");

~removeIndex && array.splice(removeIndex, 1);

答案 2 :(得分:25)

findIndex适用于现代浏览器:

var myArr = [{id:'a'},{id:'myid'},{id:'c'}];
var index = arr.findIndex(function(o){
     return o.id === 'myid';
})
if (index !== -1) myArr.splice(index, 1);

答案 3 :(得分:24)

使用lodash /下划线:

如果您想修改现有阵列本身,那么我们必须使用拼接。以下是使用 findWhere 下划线/ lodash的更好/可读方式:

var items= [{id:'abc',name:'oh'}, // delete me
                  {id:'efg',name:'em'},
                  {id:'hij',name:'ge'}];

items.splice(_.indexOf(items, _.findWhere(items, { id : "abc"})), 1);

使用ES5或更高版本

没有lodash /下划线

从ES5开始,我们在数组上有findIndex方法,所以很容易没有lodash / underscore

items.splice(items.findIndex(function(i){
    return i.id === "abc";
}), 1);

(几乎所有现代浏览器都支持ES5)

About findIndex及其Browser compatibility

答案 4 :(得分:9)

如果您只想将其从现有阵列中删除而不是创建新阵列,请尝试:

var items = [{Id: 1},{Id: 2},{Id: 3}];
items.splice(_.indexOf(items, _.find(items, function (item) { return item.Id === 2; })), 1);

答案 5 :(得分:5)

  

请只使用原生JavaScript。

作为替代方案,使用ECMAScript 5的更多“功能”解决方案,您可以使用:

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

arrayOfObjects.reduceRight(function(acc, obj, idx) {
    if (listToDelete.indexOf(obj.id) > -1)
        arrayOfObjects.splice(idx,1);
}, 0); // initial value set to avoid issues with the first item and
       // when the array is empty.

console.log(arrayOfObjects);
[ { id: 'hij', name: 'ge' } ]

根据the definition of 'Array.prototype.reduceRight' in ECMA-262

  

reduceRight不会直接改变名为的对象,但是对象可能会被callbackfn 的调用所突变。

因此,这是reduceRight的有效用法。

答案 6 :(得分:4)

通过递减i来反向循环以避免问题:

for (var i = arrayOfObjects.length - 1; i >= 0; i--) {
    var obj = arrayOfObjects[i];

    if (listToDelete.indexOf(obj.id) !== -1) {
        arrayOfObjects.splice(i, 1);
    }
}

或使用filter

var newArray = arrayOfObjects.filter(function(obj) {
    return listToDelete.indexOf(obj.id) === -1;
});

答案 7 :(得分:2)

var arrayOfObjects = [{id:'abc',name:'oh'}, // delete me
                      {id:'efg',name:'em'}, // delete me
                      {id:'hij',name:'ge'}] // all that should remain

根据你的回答将是这样的。当你单击某个特定对象时,在param中发送删除我的函数的索引。这个简单的代码就像魅力一样。

function deleteme(i){
    if (i > -1) {
      arrayOfObjects.splice(i, 1);
    }
}

答案 8 :(得分:2)

使用Set和ES6过滤器检查出来。

  let result = arrayOfObjects.filter( el => (-1 == listToDelete.indexOf(el.id)) );
  console.log(result);

这是JsFiddle: https://jsfiddle.net/jsq0a0p1/1/

答案 9 :(得分:1)

带有过滤器和indexOf

withLodash = _.filter(arrayOfObjects, (obj) => (listToDelete.indexOf(obj.id) === -1));
withoutLodash = arrayOfObjects.filter(obj => listToDelete.indexOf(obj.id) === -1);

带有过滤器和包含项

withLodash = _.filter(arrayOfObjects, (obj) => (!listToDelete.includes(obj.id)))
withoutLodash = arrayOfObjects.filter(obj => !listToDelete.includes(obj.id));

答案 10 :(得分:0)

如果您喜欢简短的自我描述性参数,或者您不想使用splice并使用直接过滤器,或者您只是像我这样的SQL人员:

function removeFromArrayOfHash(p_array_of_hash, p_key, p_value_to_remove){
    return p_array_of_hash.filter((l_cur_row) => {return l_cur_row[p_key] != p_value_to_remove});
}

示例用法:

l_test_arr = 
[
    {
         post_id: 1,
        post_content: "Hey I am the first hash with id 1"
    },
    {
        post_id: 2,
        post_content: "This is item 2"
    },
    {
        post_id: 1,
        post_content: "And I am the second hash with id 1"
    },
    {
        post_id: 3,
        post_content: "This is item 3"
    },
 ];



 l_test_arr = removeFromArrayOfHash(l_test_arr, "post_id", 2); // gives both of the post_id 1 hashes and the post_id 3
 l_test_arr = removeFromArrayOfHash(l_test_arr, "post_id", 1); // gives only post_id 3 (since 1 was removed in previous line)

答案 11 :(得分:0)

要通过给定数组中的ID删除对象;

const hero = [{'id' : 1, 'name' : 'hero1'}, {'id': 2, 'name' : 'hero2'}];
//remove hero1
const updatedHero = hero.filter(item => item.id !== 1);

答案 12 :(得分:0)

您可以使用filter。如果条件为true,则此方法始终返回元素。因此,如果要按ID删除,则必须保留所有与给定ID不匹配的元素。这是一个示例:

arrayOfObjects = arrayOfObjects.filter(obj => obj.id!= idToRemove)

答案 13 :(得分:-1)

var apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My New App',another:'things'}]

var removeIndex = apps.map(function(item) { return item.id; }).indexOf(37)

apps.splice(removeIndex, 1);