我一直在尝试几种方法来查找数组中的对象,其中ID = var,如果找到,则从数组中删除对象并返回新的对象数组。
数据:
[
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
]
我可以使用jQuery $ grep搜索数组;
var id = 88;
var result = $.grep(data, function(e){
return e.id == id;
});
但是,当id == 88时,如何删除整个对象,并返回如下数据:
数据:
[
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
]
我可以grep数组的id,但如何删除id == 88的整个对象
只需通过相反的谓词过滤即可:
var data = $.grep(data, function(e){
return e.id != id;
});
如果您不使用jQuery,这是一个解决方案:
myArray = myArray.filter(function( obj ) {
return obj.id !== id;
});
您可以简化此过程,这里实际上不需要使用jquery。
var id = 88;
for(var i = 0; i < data.length; i++) {
if(data[i].id == id) {
data.splice(i, 1);
break;
}
}
只需遍历列表,找到匹配的ID,进行拼接,然后中断即可退出循环
在ES6 / 2015中,有一种使用findIndex和array spread运算符的新方法:
const index = data.findIndex(obj => obj.id === id);
const newData = [
...data.slice(0, index),
...data.slice(index + 1)
]
您可以将其变成一个函数,以供以后重用,如下所示:
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;
}
这样,您可以使用一种方法通过不同的键删除项目(如果没有满足条件的对象,则返回原始数组):
const newData = remove(data, "id", "88");
const newData2 = remove(data, "name", "You are awesome!");
或者,您可以将其放在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;
};
并以这种方式使用它:
const newData = data.remove("id", "88");
const newData2 = data.remove("name", "You are awesome!");
假设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);
var items = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
];
如果您使用的是jQuery,请像这样使用jQuery.grep:
items = $.grep(items, function(item) {
return item.id !== '88';
});
// items => [{ id: "99" }, { id: "108" }]
使用ES5 Array.prototype.filter:
items = items.filter(function(item) {
return item.id !== '88';
});
// items => [{ id: "99" }, { id: "108" }]
也许您正在寻找$.grep()
功能:
arr = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
];
id = 88;
arr = $.grep(arr, function(data, index) {
return data.id != id
});
sift
是一个功能强大的集合过滤器,用于此类操作以及更高级的操作。它在node.js的浏览器或服务器端的客户端起作用。
var collection = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
];
var sifted = sift({id: {$not: 88}}, collection);
它支持像过滤器$in
,$nin
,$exists
,$gte
,$gt
,$lte
,$lt
,$eq
,$ne
,$mod
,$all
,$and
,$or
,$nor
,$not
,$size
,$type
,和$regex
,并努力成为API兼容的MongoDB集合过滤。
Array.prototype.removeAt = function(id) {
for (var item in this) {
if (this[item].id == id) {
this.splice(item, 1);
return true;
}
}
return false;
}
这应该可以解决问题,jsfiddle
如果测试严格相等,请确保将对象ID强制为整数:
var result = $.grep(data, function(e, i) {
return +e.id !== id;
});
如果您使用下划线js,则很容易根据键删除对象。
http://underscorejs.org。例:
var temp1=[{id:1,name:"safeer"}, //temp array
{id:2,name:"jon"},
{id:3,name:"James"},
{id:4,name:"deepak"},
{id:5,name:"ajmal"}];
var id = _.pluck(temp1,'id'); //get id array from temp1
var ids=[2,5,10]; //ids to be removed
var bool_ids=[];
_.each(ids,function(val){
bool_ids[val]=true;
});
_.filter(temp1,function(val){
return !bool_ids[val.id];
});
同意答案,这是一种简单的方法,如果您想通过id查找和反对并删除它,就像下面的代码一样。
var obj = JSON.parse(data);
var newObj = obj.filter(item=>item.Id!=88);
文章标签:arrays , javascript , jquery , object
版权声明:本文为原创文章,版权归 javascript 所有,欢迎分享本文,转载请保留出处!
评论已关闭!