Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript从数组中删除对象(Remove Object from Array using JavaScript)

How can I remove an object from an array?

(如何从数组中删除对象?)

I wish to remove the object that includes name Kristian from someArray .

(我希望从someArray删除包含名称Kristian的对象。)

For example:

(例如:)

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

I want to achieve:

(我要实现:)

someArray = [{name:"John", lines:"1,19,26,96"}];
  ask by Clem translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use several methods to remove item(s) from an Array:

(您可以使用多种方法从数组中删除项目:)

//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

If you want to remove element at position x , use:

(如果要删除位置x处的元素,请使用:)

someArray.splice(x, 1);

Or

(要么)

someArray = someArray.slice(0, x).concat(someArray.slice(-x));

Reply to the comment of @chill182 : you can remove one or more elements from an array using Array.filter , or Array.splice combined with Array.findIndex (see MDN ), eg

(回复@ chill182的评论:您可以使用Array.filterArray.spliceArray.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> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...