如何从数组中删除对象? 我希望从someArray中删除包含名称Kristian的对象。例如:
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
我想实现:
someArray = [{name:"John", lines:"1,19,26,96"}];
如何从数组中删除对象? 我希望从someArray中删除包含名称Kristian的对象。例如:
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
我想实现:
someArray = [{name:"John", lines:"1,19,26,96"}];
当前回答
const someArray = [{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"}];
我们得到对象的索引它的name属性值为"Kristian"
const index = someArray.findIndex(key => key.name === "Kristian");
console.log(index); // 0
通过使用拼接函数我们删除了name属性值为“Kristian”的对象
someArray.splice(index,1);
console.log(someArray); // [{name:"John", lines:"1,19,26,96"}]
其他回答
我做了一个动态函数,以对象数组,键和值,并返回相同的数组后删除所需的对象:
function removeFunction (myObjects,prop,valu)
{
return myObjects.filter(function (val) {
return val[prop] !== valu;
});
}
完整示例:DEMO
var obj = {
"results": [
{
"id": "460",
"name": "Widget 1",
"loc": "Shed"
}, {
"id": "461",
"name": "Widget 2",
"loc": "Kitchen"
}, {
"id": "462",
"name": "Widget 3",
"loc": "bath"
}
]
};
function removeFunction (myObjects,prop,valu)
{
return myObjects.filter(function (val) {
return val[prop] !== valu;
});
}
console.log(removeFunction(obj.results,"id","460"));
在数组上使用splice函数。指定开始元素的位置和要删除的子序列的长度。
someArray.splice(pos, 1);
只从数组中返回属性名不是“Kristian”的对象
var noKristianArray = $.grep(someArray, function (el) { return el.name!= "Kristian"; });
演示:
var someArray = [ {名称:“克里斯蒂安”,行:“2、5、10”}, {名称:“约翰”,行:“1,19日,26日96”}, {名称:“克里斯蒂安”,行:" 58160 "}, {名称:“费利克斯”,行:“96”1,19日,26日} ]; var noKristianArray = $。grep(someArray, function (el){返回el.name!= "克里斯蒂安”;}); console.log (noKristianArray); < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本>
你可以使用array.filter()。
如。
someArray = [{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"}];
someArray = someArray.filter(function(returnableObjects){
return returnableObjects.name !== 'Kristian';
});
//someArray will now be = [{name:"John", lines:"1,19,26,96"}];
箭头功能:
someArray = someArray.filter(x => x.name !== 'Kristian')
你可以使用以下几种方法从数组中删除项:
//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, someArray.length - 1); // last element removed
//6
someArray.length = someArray.length - 1; // last element removed
如果你想移除x位置的元素,使用:
someArray.splice(x, 1);
Or
someArray = someArray.slice(0, x).concat(someArray.slice(-x));
回复@chill182的评论:您可以使用array从数组中删除一个或多个元素。过滤器或数组。拼接结合数组。findIndex(参见MDN)。
请看这个Stackblitz项目或下面的代码片段:
// non destructive filter > noJohn = John removed, but someArray will not change let someArray = getArray(); let noJohn = someArray.filter( el => el.name !== "John" ); log(`let noJohn = someArray.filter( el => el.name !== "John")`, `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("", `someArray2 = someArray2.filter( el => el.name !== "John" )`, `destructive filter/reassign John removed [someArray2] =`, format(someArray2)); log(`**someArray2.length after filter ${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("", `someArray3.splice(someArray3.findIndex(v => v.name === "Kristian"), 1),`, `destructive splice /w findIndex Brian remains [someArray3] =`, format(someArray3)); log(`**someArray3.length after splice ${someArray3.length}`); // if you're not sure about the contents of your array, // you should check the results of findIndex first let someArray4 = getArray(); const indx = someArray4.findIndex(v => v.name === "Michael"); someArray4.splice(indx, indx >= 0 ? 1 : 0); log("", `someArray4.splice(indx, indx >= 0 ? 1 : 0)`, `check findIndex result first [someArray4] = (nothing is removed)`, format(someArray4)); log(`**someArray4.length (should still be 3) ${someArray4.length}`); // -- helpers -- 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>