0

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

1
  • It would be much more efficient to store the IDs as keys of an object so you don't have to search for them every time. The value could be the index of the associated object in the array. Commented Jan 18, 2018 at 22:45

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you.I am new to javascript and I looked at Array.prototype.reduce. can you please help me explain your code
1

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.

1 Comment

My answer used String::equals() rather than '==', and someone edited it to use '=='. The '==' operator when applied to objects only tests object identity, not object equality. Java runtimes use a String cache such that two Strings with the same content would actually be the same object. But (a) I don't know if this is guaranteed in all runtimes, and (b) I would expect equals() to take advantage of this if it were.
0
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.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.