简体   繁体   English

检查对象数组中是否存在具有特定键值的对象

[英]check if an object with a specific key value exists in an array of objects

I'm trying to check if an array of items contains an object with a specific key value, and if it does, just update the array of items with the new item object 我正在尝试检查项目数组是否包含具有特定键值的对象,如果包含,请使用新的项目对象更新项目数组

[{name: example1, quantity: 2},{name: example2, quantity: 3},{name: example3, quantity: 5}]

so if for example I'm pushing an object with the same name of "example1", I want to check if it does exist in the array and if it does, just update it with the new object with the new quantity for example. 因此,例如,如果我要推送一个名称与“ example1”相同的对象,那么我想检查它是否确实存在于数组中,如果存在,则只需使用具有新数量的新对象进行更新。

I tried to explain what I want as best as I can, please let me know if you need more clarification. 我试图尽我所能解释,如果您需要更多说明,请告诉我。

You can use a simple combination of Array#some() and Array#find() methods: 您可以使用Array#some()Array#find()方法的简单组合:

if (data.some(o => o.name == toPush.name)) {
  data.find(o => o.name == toPush.name).quantity = toPush.quantity;
}

Where data is your array and toPush is the object you are trying to update. data是您的arraytoPush是您要更新的object

Demo: 演示:

 var data = [{ name: "example1", quantity: 2 }, { name: "example2", quantity: 3 }, { name: "example3", quantity: 5 }]; let toPush = { name: "example2", quantity: 10 }; if (data.some(o => o.name == toPush.name)) { data.find(o => o.name == toPush.name).quantity = toPush.quantity; } console.log(data); 

let arr1 = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}];
let obj1 = {name: 'example1', quantity: 5};
if(arr1.filter(item=> item.name === obj1.name).length==0){
  arr1.push(obj1);
}
else{
  arr1.filter(item=> item.name === obj1.name)[0].quantity = obj1.quantity;
}

You can use Array.find() for that update and pushing the unique objects. 您可以使用Array.find()进行更新并推送唯一对象。 You can also use existObj = obj; 您也可以使用existObj = obj; so that all the updated properties of obj is set on the existing object and not just one property (as it is now with quantity ). 因此obj所有更新属性都将设置在现有对象上,而不仅仅是一个属性(因为现在具有quantity )。 But if you need only the quantity property to get updated then use existObj.quantity = obj.quantity; 但是,如果只需要quantity属性进行更新,则可以使用existObj.quantity = obj.quantity;

 let arr = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}]; let obj = {name: 'example1', quantity: 5}; var existObj = arr.find(({name}) => name === obj.name); if(existObj){ existObj = obj; } else { arr.push(obj); } console.log(arr); 

I'd use the Array.prototype.some() function: 我会使用Array.prototype.some()函数:

const arr = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}]


var result = arr.some(e => e.hasOwnProperty('name') && e.hasOwnProperty('quantity'));

console.log("The array contains an object with a 'name' and 'quantity' property: " + result);

Better use Array.findIndex() with this 最好与此一起使用Array.findIndex()

let myArray = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}];
const newItem = { name: 'example1', quantity: 5 };
const indexOfItem = myArray.findIndex(item => item.name === item.name);

if(indexOfItem === -1) { // not existing
  myArray.push(newItem);
else {
  myArray[indexOfItem] = newItem;
} 

 var ObjectArray = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}] console.log(ObjectArray) var name = 'example2' var index = ObjectArray.findIndex(product => product.name == name); if(index>=0){ console.log("Found and updated ObjectArray") ObjectArray.splice(index, 1, {name: 'example2', quantity: 8}); console.log(ObjectArray) } else{ console.log("not found") } 

In case if you want to match both object key & values, this will work. 如果您想同时匹配对象键和值,则可以使用。

let myArray = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}];
let newObj = { name: 'example1', quantity: 5 };

let myArrayJSON = JSON.stringify(myArray);
let newObjJSON = JSON.stringify(newObj);

if(myArrayJSON.indexOf(newObjJSON) === -1){
   myArray.push(newObj); 
}

You can create "push" function and pass in the object you want to add/update. 您可以创建“推”功能,并传入要添加/更新的对象。 Function will take care if it needs to add / update arr by using " find ". 如果需要通过使用“ find ”添加/更新arr,函数将谨慎处理。 Also, this will take care of all properties not just "quantity" 另外,这将照顾所有属性,而不仅仅是“数量”

 var arr = [{name: 'example1', quantity: 2},{name: 'example2', quantity: 3},{name: 'example3', quantity: 5}] function push(obj) { var data = arr.find(({name}) => name == obj.name) || (arr = arr.concat({})).slice(-1)[0]; Object.assign(data, obj) } push({ name: 'example11', quantity: 100 }) console.log('example11 added', arr) push({ name: 'example1', quantity: 100 }) console.log('example1 updated', arr) 

you can use Array.forEach which iterates through each object, and we can update existing array with using the index param. 您可以使用Array.forEach遍历每个对象,并且可以使用索引参数更新现有数组。

Please see the below code. 请参见下面的代码。

 var data = [{name: "example1", quantity: 2},{name: "example2", quantity: 3},{name: "example3", quantity: 5}] var object = {name: "example1", quantity: 14} data.forEach((o,index) => { if(o.name === object.name){ o.quantity = object.quantity return data[index] = o }else{ data[index] = o } }) console.log("updated data array =>", data) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM