Home >Web Front-end >JS Tutorial >What are the methods to delete arrays in JavaScript?
JavaScript array deletion method: 1. Delete by index, the code is [someArray.shift()]; 2. Delete by condition, the code is [johnRemoved = someArray.filter(function(el)].
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, DELL G3 computer.
How to delete JavaScript arrays:
Delete by index
//1 someArray.shift(); // 删除第一个元素 //2 someArray = someArray.slice(1); // 删除第一个元素 //3 someArray.splice(0,1); // 删除第一个元素 //4 someArray.pop(); // 删除最后一个元素
Delete by condition
someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}, {name:"Brian",lines:"3,9,62,36" }]; johnRemoved = someArray.filter(function(el) { return el.name !== "John"; }); console.log(JSON.stringify(johnRemoved, null, ' '));
Related free learning recommendations :javascript video tutorial
The above is the detailed content of What are the methods to delete arrays in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!