[英]Javascript: Remove Value from array inside an array of Object based on key
[英]javascript find and remove object in array based on key value
我一直在嘗試幾種關於如何在數組中查找對象的方法,其中 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!"}
]
如果您不使用 jquery,這里有一個解決方案:
myArray = myArray.filter(function( obj ) {
return obj.id !== id;
});
我可以 grep id 的數組,但是如何刪除 id == 88 的整個對象
只需按相反的謂詞過濾:
var data = $.grep(data, function(e){
return e.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,拼接,然后 break 退出循環
ES6/2015 中有一種新方法可以使用 findIndex 和數組展開運算符來執行此操作:
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!");
假設 ids 是唯一的,你只需要刪除一個元素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" }]
原生 ES6 解決方案:
const pos = data.findIndex(el => el.id === ID_TO_REMOVE);
if (pos >= 0)
data.splice(pos, 1);
如果您確定該元素在數組中:
data.splice(data.findIndex(el => el.id === ID_TO_REMOVE), 1);
原型:
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);
http://jsfiddle.net/oriadam/72kgprw5/
注意:這將就地刪除項目。 如果您需要一個新數組,請使用前面答案中提到的filter
。
也許您正在尋找$.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
});
Array.prototype.removeAt = function(id) {
for (var item in this) {
if (this[item].id == id) {
this.splice(item, 1);
return true;
}
}
return false;
}
這應該可以解決問題, jsfiddle
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
,並努力與 MongoDB 集合過濾的 API 兼容。
const data = [
{"id":"88","name":"Lets go testing"},
{"id":"99","name":"Have fun boys and girls"},
{"id":"108","name":"You are awesome!"}
];
這里我們得到id值為“88”的對象的索引
const index = data.findIndex(item => item.id === "88");
console.log(index); // 0
我們使用 splice 函數從數據數組中刪除指定的對象
data.splice(index,1);
console.log(data); // [{"id":"99","name":"Have fun boys and girls"},{"id":"108","name":"You are awesome!"}]
我同意這些答案,如果您想通過 id 查找對象並將其刪除,那么簡單的方法就像下面的代碼。
var obj = JSON.parse(data);
var newObj = obj.filter(item=>item.Id!=88);
如果您使用下划線 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];
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.