如何从数组中删除对象? 我希望从someArray
删除包含名称Kristian
的对象。 例如:
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
我要实现:
someArray = [{name:"John", lines:"1,19,26,96"}];
#1楼
在数组上使用拼接功能。 指定起始元素的位置和要删除的子序列的长度。
someArray.splice(pos, 1);
#2楼
您可以使用多种方法从数组中删除项目:
//1
someArray.shift(); // first element removed
//2
someArray = someArray.slice(1); // first element removed
//3
someArray.splice(0, 1); // first element removed
//4
someArray.pop(); // last element removed
//5
someArray = someArray.slice(0, a.length - 1); // last element removed
//6
someArray.length = someArray.length - 1; // last element removed
如果要删除位置x
处的元素,请使用:
someArray.splice(x, 1);
要么
someArray = someArray.slice(0, x).concat(someArray.slice(-x));
回复@ chill182的评论:您可以使用Array.filter
或Array.splice
与Array.findIndex
组合(请参见MDN )从数组中删除一个或多个元素,例如
// non destructive filter > noJohn = John removed, but someArray will not change let someArray = getArray(); let noJohn = someArray.filter( el => el.name !== "John" ); log("non destructive filter > noJohn = ", format(noJohn)); log(`**someArray.length ${someArray.length}`); // destructive filter/reassign John removed > someArray2 = let someArray2 = getArray(); someArray2 = someArray2.filter( el => el.name !== "John" ); log("", "destructive filter/reassign John removed > someArray2 =", format(someArray2)); log(`**someArray2.length ${someArray2.length}`); // destructive splice /w findIndex Brian remains > someArray3 = let someArray3 = getArray(); someArray3.splice(someArray3.findIndex(v => v.name === "Kristian"), 1); someArray3.splice(someArray3.findIndex(v => v.name === "John"), 1); log("", "destructive splice /w findIndex Brian remains > someArray3 =", format(someArray3)); log(`**someArray3.length ${someArray3.length}`); function format(obj) { return JSON.stringify(obj, null, " "); } function log(...txt) { document.querySelector("pre").textContent += `${txt.join("\\n")}\\n` } function getArray() { return [ {name: "Kristian", lines: "2,5,10"}, {name: "John", lines: "1,19,26,96"}, {name: "Brian", lines: "3,9,62,36"} ]; }
<pre> **Results** </pre>
#3楼
干净的解决方案是使用Array.filter
:
var filtered = someArray.filter(function(el) { return el.Name != "Kristian"; });
这样做的问题是它不能在IE <9上运行。但是,您可以包括来自Java库的代码(例如underscore.js ),该代码可为任何浏览器实现此功能。
#4楼
最简单的解决方案是创建一个按名称存储每个对象的索引的映射,如下所示:
//adding to array
var newPerson = {name:"Kristian", lines:"2,5,10"}
someMap[ newPerson.name ] = someArray.length;
someArray.push( newPerson );
//deleting from the array
var index = someMap[ ‘Kristian‘ ];
someArray.splice( index, 1 );
#5楼
所示的“数组”是无效的JavaScript语法。 花括号{}
适用于具有属性名称/值对的对象,而方括号[]
适用于数组-如下所示:
someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
在这种情况下,可以使用.splice()
方法删除项目。 要删除第一项(索引0),请说:
someArray.splice(0,1);
// someArray = [{name:"John", lines:"1,19,26,96"}];
如果您不知道索引,但想在数组中搜索以找到要删除的名称为“ Kristian”的项目,则可以这样做:
for (var i =0; i < someArray.length; i++)
if (someArray[i].name === "Kristian") {
someArray.splice(i,1);
break;
}
编辑:我只是注意到您的问题被标记为“ jQuery”,所以您可以尝试$.grep()
方法 :
someArray = $.grep(someArray,
function(o,i) { return o.name === "Kristian"; },
true);
来源:SEO技术
原文地址:https://www.cnblogs.com/1994jinnan/p/12203324.html
时间: 2024-10-12 15:35:49