如何从数组中删除对象? 我希望从someArray中删除包含名称Kristian的对象。例如:

someArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"John", lines:"1,19,26,96"}];

我想实现:

someArray = [{name:"John", lines:"1,19,26,96"}];

当前回答

在数组上使用splice函数。指定开始元素的位置和要删除的子序列的长度。

someArray.splice(pos, 1);

其他回答

在数组上使用splice函数。指定开始元素的位置和要删除的子序列的长度。

someArray.splice(pos, 1);

虽然这可能不适合这种情况,我发现前几天,如果你不需要改变数组的大小,你也可以使用delete关键字从数组中删除一个项目。

var myArray = [1,2,3];

delete myArray[1];

console.log(myArray[1]); //undefined

console.log(myArray.length); //3 - doesn't actually shrink the array down

在你的数组语法中似乎有一个错误,所以假设你的意思是一个数组,而不是一个对象,数组。Splice是你的朋友:

someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
someArray.splice(1,1)

ES2015

let someArray = [
               {name:"Kristian", lines:"2,5,10"},
               {name:"John", lines:"1,19,26,96"},
               {name:"Kristian", lines:"2,58,160"},
               {name:"Felix", lines:"1,19,26,96"}
            ];

someArray = someArray.filter(person => person.name != 'John');

它会除掉约翰!

someArray = jQuery.grep(someArray , function (value) {
        return value.name != 'Kristian';
});