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.
I tried to explain what I want as best as I can, please let me know if you need more clarification.
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.
I tried to explain what I want as best as I can, please let me know if you need more clarification.
Share Improve this question edited Oct 26, 2018 at 10:36 cнŝdk 32.1k7 gold badges60 silver badges80 bronze badges asked Oct 26, 2018 at 9:50 develop05develop05 4851 gold badge9 silver badges24 bronze badges 2-
Do these objects only have
name
andquantity
keys? If so, then you should just use a mapping, not an array of objects. If there's more keys in the objects, what would happen to those keys when you merge? – Håken Lid Commented Oct 26, 2018 at 9:55 - 1 You should really add a) a before/after example of your array of objects (because, based on the answers, I'm not sure people understand what you want, and b) your own attempted solution. – Andy Commented Oct 26, 2018 at 10:03
9 Answers
Reset to default 5You can use a simple bination of Array#some()
and Array#find()
methods:
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.
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. You can also use 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
). But if you need only the quantity
property to get updated then use 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:
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
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;
}
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". 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.
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)
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);
}
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")
}