-2

I've the below array of objects. How do I iterate over it to change inventory and unit_price if product name is found, and create new product if the name is no found. for example, if in my_product the name is stool as shown, this record to be added to the array, but if the name is, let's say table then the inventory and unit_price of product table are required to be adjusted.

let products = [
  {
    name: "chair",
    inventory: 5,
    unit_price: 45.99
  },
  {
    name: "table",
    inventory: 10,
    unit_price: 123.75
  },
  {
    name: "sofa",
    inventory: 2,
    unit_price: 399.50
  }
];

let my_product = {name: "stool", inventory: 1, unit_price: 300}
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
  • Just find the entry in the array (see the [linked question's](https://stackoverflow.com/questions/18131434/how-to-find-an-appropriate-object-in-array-by-one-of-its-properties) answers) then, if you found it, adjust it; if not, add your new entry. See the answers there ([one of which is mine](https://stackoverflow.com/a/47847612/157247) ;-) ). – T.J. Crowder Jul 09 '19 at 16:56

5 Answers5

2

Quick and dirty, there's probably a more concise answer.

    let products = [
        {
            name: "chair",
            inventory: 5,
            unit_price: 45.99
        },
        {
            name: "table",
            inventory: 10,
            unit_price: 123.75
        },
        {
            name: "sofa",
            inventory: 2,
            unit_price: 399.50
        }
    ];

    let my_product = { name: "stool", inventory: 1, unit_price: 300 }

    let found = products.findIndex((e) => e.name == my_product.name);
    if (found === -1) {
        products.push(my_product);
    } else {
        const index = products.findIndex((e) => e.name === my_product.name);
        products[index].inventory = my_product.inventory;
        products[index].unit_price = my_product.unit_price;
    }
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
Iskandar Reza
  • 953
  • 1
  • 7
  • 16
1

use find to find the name in the products array. If found update the required properties else push to array.

let products = [{
    name: "chair",
    inventory: 5,
    unit_price: 45.99
  },
  {
    name: "table",
    inventory: 10,
    unit_price: 123.75
  },
  {
    name: "sofa",
    inventory: 2,
    unit_price: 399.50
  }
];

let my_product = {
  name: "table",
  inventory: 1,
  unit_price: 300
};

        let found = products.findIndex((e) => e.name == my_product.name);
    if (found === -1) {
        products.push(my_product);
    } else {
        const index = products.findIndex((e) => e.name === my_product.name);
        products[index].inventory = my_product.inventory;
        products[index].unit_price = my_product.unit_price;
    }

console.log(products);
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
random
  • 7,756
  • 3
  • 19
  • 25
0

This is a perfect use case for a map (docs), keyed by the product name. It will handle everything for you.

let product = {};
product["stool"] = {inventory: 1, unit_price: 300}; // new object is created
product["stool"] = {inventory: 5, unit_price: 350}; // stool data is replaced

// to find object, just look it up by product name
let my_stool = product["stool"];  // will return 'undefined' if "stool" doesn't exist
Kon
  • 4,023
  • 4
  • 24
  • 38
0

You can use forEach to iterate over the elements. If the name matches then merge two objects using spread operator.

let products = [
  {
    name: "chair",
    inventory: 5,
    unit_price: 45.99
  },
  {
    name: "table",
    inventory: 10,
    unit_price: 123.75
  },
  {
    name: "sofa",
    inventory: 2,
    unit_price: 399.50
  }
];
let my_product = {name: "stool", inventory: 1, unit_price: 300}
let added = false;
products.forEach((x,i) => {
  if(x.name === my_product.name){
    added = true;
    products[i] = {...x,...my_product};
  }
})
if(!added){
  products.push(my_product);
}
console.log(products)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
-3

Loop through the array:

for (index in products) {

At each index, check the name:

if (products[index].name==='chair') {
...

You can also include a boolean flag for if you have found 'chair'. If not, and index is at products.length-1, then add the new element to the object.

avikarto
  • 101
  • 5