e.iluf
e.iluf

Reputation: 1659

How to update array specific attribute of an array object

I want to check an array and find the array that contains a certain thing

I have a cartarray that contains these values

  {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}

  {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"}

  {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"}

  {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}

  {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}

each of the object represent a product what I really want to do is prevent duplication of id so if the id are the same, I want to consolidate the quantities. so before I add a new product object of this format

{id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "7"}

I want to check if there is similar productid in the cartarray

var arrayLength = cartarry.length;
for (var i = 0; i < arrayLength; i++) { 
    if (cartarry[i] == product.id ){
        console.log("we got a match")           
        var updatedquantity = quantity + parseInt(product.quantity) 
    }
}

I tried couple of different method but unsuccessful. How can i find the matching id and update the quantity ? I hope i am clear in my description

Upvotes: 0

Views: 78

Answers (4)

dhilt
dhilt

Reputation: 20744

To consolidate your product quantities by id you want to use Array.prototype.reduce:

let data = [
  {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
  {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
  {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"},
  {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"},
  {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}
];

data = data.reduce((acc, item) => {
  const foundItem = acc.find(_item => _item.id === item.id);
  if(foundItem) {
    foundItem.quantity = Number(item.quantity) + Number(foundItem.quantity) + '';
  }
  else {
    acc.push(item);
  }
  return acc;
}, []);

console.log(data);

//0: {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "2"}
//1: {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "5"}

UPD. With the help of the reduce method we want to get a processed copy of original data array. The output array is being formed as acc accumulator value, which is initially an empty array (this is regulated by the last parameter of reduce: []). Each item of initial data set is being considered separately as item local variable inside a callback, and we are changing acc in accordance with the current item contents. We are trying to find current item in current acc array by id to consolidate quantities. Otherwise, if current item is unique for current acc array, we are pushing current item to acc.

Upvotes: 1

vadzim dvorak
vadzim dvorak

Reputation: 959

Try this approach:

 var grouped = [];
 var array = [{id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
      {id: "1510-01-312-3501-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514542566148", name: "AIRPLANE UTILITY", price: "$90", quantity: "1"},
      {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "3"},
      {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"},
      {id: "1510-00-033-6312-OkqcPp3xJwfgmNinwGsKZmAa8xt1-1514540733034", name: "AIRPLANE UTILITY", price: "$43", quantity: "1"}]

array.forEach(function (o) {
    if (!this[o.id]) {
        this[o.id] = { id: o.id, name:o.name, price:o.price, quantity: 0 };
        grouped.push(this[o.id]);
    }
    this[o.id].quantity += parseInt(o.quantity);
}, Object.create(null));

console.log(grouped);

Upvotes: 0

Steve Brandli
Steve Brandli

Reputation: 566

I'll chip away at the problem. The line

if (cartarray[i] == product.id) {

should be

if (cartarray[i].id == product.id) {

Also, if there can be only zero or one cartarray entry that matches product.id, consider putting a break after your accumulator.

Upvotes: 1

aaronofleonard
aaronofleonard

Reputation: 2576

var arrayLength = cartarry.length;
var match = false;
for (var i = 0; i < arrayLength; i++) { 
    if (cartarry[i].id == product.id ) {
       console.log("we got a match")

       cartarry[i].quantity += parseInt(product.quantity);
       match = true;
       break; // because this can only happen once per array
    }
}
if (!match) {
    // add it to the array
}

There's not a whole lot of information in your post so I guessed at a couple things. The key details are:

  • You can't just compare product.id to cartarry[i]. cartarry[i] contains an object with the fields id, name, price, and quantity. You have to make use of those fields for the comparison, as well as updating.

Upvotes: 0

Related Questions