Tom*_*Tom 115 javascript arrays jquery object
我一直在尝试几种方法来查找数组中的对象,其中ID = var,如果找到,则从数组中删除对象并返回新的对象数组.
数据:
[
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
]
Run Code Online (Sandbox Code Playgroud)
我可以使用jQuery $ grep搜索数组;
var id = 88;
var result = $.grep(data, function(e){
return e.id == id;
});
Run Code Online (Sandbox Code Playgroud)
但是如何在id == 88时删除整个对象,并返回如下数据:
数据:
[
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
]
Run Code Online (Sandbox Code Playgroud)
Ber*_*rgi 141
我可以为数组grep数组,但是如何删除id == 88的整个对象
只需按相反的谓词过滤:
var data = $.grep(data, function(e){
return e.id != id;
});
Run Code Online (Sandbox Code Playgroud)
Ada*_*ani 117
如果您不使用jquery,这是一个解决方案:
myArray = myArray.filter(function( obj ) {
return obj.id !== id;
});
Run Code Online (Sandbox Code Playgroud)
Bry*_*yan 81
你可以简化这个,而且这里真的不需要使用jquery.
var id = 88;
for(var i = 0; i < data.length; i++) {
if(data[i].id == id) {
data.splice(i, 1);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
只需遍历列表,找到匹配的id,拼接,然后中断以退出循环
zor*_*rza 21
使用findIndex和数组扩展运算符在ES6/2015中有一种新方法:
const index = data.findIndex(obj => obj.id === id);
const newData = [
...data.slice(0, index),
...data.slice(index + 1)
]
Run Code Online (Sandbox Code Playgroud)
你可以把它变成一个函数,以便以后重用,如下所示:
function remove(array, key, value) {
const index = array.findIndex(obj => obj[key] === value);
return index >= 0 ? [
...array.slice(0, index),
...array.slice(index + 1)
] : array;
}
Run Code Online (Sandbox Code Playgroud)
这样,您可以使用一种方法通过不同的键删除项目(如果没有符合条件的对象,则返回原始数组):
const newData = remove(data, "id", "88");
const newData2 = remove(data, "name", "You are awesome!");
Run Code Online (Sandbox Code Playgroud)
或者你可以把它放在你的Array.prototype上:
Array.prototype.remove = function (key, value) {
const index = this.findIndex(obj => obj[key] === value);
return index >= 0 ? [
...this.slice(0, index),
...this.slice(index + 1)
] : this;
};
Run Code Online (Sandbox Code Playgroud)
并以这种方式使用它:
const newData = data.remove("id", "88");
const newData2 = data.remove("name", "You are awesome!");
Run Code Online (Sandbox Code Playgroud)
假设id是唯一的,你只需删除一个元素splice
应该做的诀窍:
var data = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
],
id = 88;
console.table(data);
$.each(data, function(i, el){
if (this.id == id){
data.splice(i, 1);
}
});
console.table(data);
Run Code Online (Sandbox Code Playgroud)
var items = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
];
Run Code Online (Sandbox Code Playgroud)
如果您使用 jQuery,请像这样使用jQuery.grep:
items = $.grep(items, function(item) {
return item.id !== '88';
});
// items => [{ id: "99" }, { id: "108" }]
Run Code Online (Sandbox Code Playgroud)
使用 ES5 Array.prototype.filter:
items = items.filter(function(item) {
return item.id !== '88';
});
// items => [{ id: "99" }, { id: "108" }]
Run Code Online (Sandbox Code Playgroud)
我同意前面的答案。如果你想通过 id 查找对象并将其删除,一个简单的方法就像下面的代码:
var obj = JSON.parse(data);
var newObj = obj.filter(item => item.Id != 88);
Run Code Online (Sandbox Code Playgroud)
小智 6
const data = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
];
Run Code Online (Sandbox Code Playgroud)
这里我们得到id值为“88”的对象的索引
const index = data.findIndex(item => item.id === "88");
console.log(index); // 0
Run Code Online (Sandbox Code Playgroud)
我们使用 splice 函数从数据数组中删除指定的对象
data.splice(index,1);
console.log(data); // [{"id":"99","name":"Have fun boys and girls"},{"id":"108","name":"You are awesome!"}]
Run Code Online (Sandbox Code Playgroud)
原生 ES6 解决方案:
const pos = data.findIndex(el => el.id === ID_TO_REMOVE);
if (pos >= 0)
data.splice(pos, 1);
Run Code Online (Sandbox Code Playgroud)
如果您确定该元素在数组中:
data.splice(data.findIndex(el => el.id === ID_TO_REMOVE), 1);
Run Code Online (Sandbox Code Playgroud)
原型:
Array.prototype.removeByProp = function(prop,val) {
const pos = this.findIndex(x => x[prop] === val);
if (pos >= 0)
return this.splice(pos, 1);
};
// usage:
ar.removeByProp('id', ID_TO_REMOVE);
Run Code Online (Sandbox Code Playgroud)
http://jsfiddle.net/oriadam/72kgprw5/
注意:这会就地删除项目。如果您需要一个新的数组,请使用filter
前面的答案中提到的。