1

I'm developing a ecommerce cart, in the following cart each item could have some "variants", the item model looks like the following where Articoli == item and each Articoli could have an array of Varianti inside it:

class Articoli {
    constructor(id, titolo, qta, prezzo, prezzo_s, img, varianti) {
        this.id = id;
        this.titolo = titolo;
        this.qta = qta;
        this.prezzo = prezzo;
        this.prezzo_s = prezzo_s;
        this.img = img;
        this.varianti = varianti;
    }
}

class Varianti {
    constructor(id, titolo, stato, prezzo) {
        this.id = id;
        this.titolo = titolo;
        this.stato = stato;
        this.prezzo = prezzo;
    }
}

Now i have to update the quantity of each added item inside the Cart. Each added item if doesn't have any Variant has id cd-cart-ID while item with variant has id cd-cart-var-ID so when two items without any variants in it are added the quantity is just updated while if the same item is added with some variants in it it's created a new item in the cart but here comes the issue i could have items with same variants added so it's quantity has to be updated, but till now i was looking for array index that has equals ID so:

var objIndex = articoli.findIndex((obj => obj.id == id))

But the ID is equals in both added items like for an item cd-cart-ID the ID equals to 7891 and even in a new added item with variants with component ID cd-cart-var-ID ID equals to 7891 in the array.

So i was going to get the objectIndex to which update the quantity by checking it for ID and for Variants inside it like this:

var objIndex = articoli.findIndex((obj => obj.id == id && obj.varianti == articolo.varianti))

Where articolo.varianti is the array of objects Varianti, but it returns in any case index -1 even if i'm adding two items without any Variants so varianti == []...

ABGR
  • 4,631
  • 4
  • 27
  • 49
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
  • 2
    can you give an example of `articolo.varianti ` where you're applying `findIndex` – ABGR Jun 08 '20 at 08:45
  • When `articolo` is an array, then it probably does not have `articolo.varianti`. You should have that in a variable,... maybe just `varianti`. – trincot Jun 08 '20 at 08:48
  • More importantly is to have the reaction to debug. Did you inspect the value of `articolo.varianti`? – trincot Jun 08 '20 at 08:49
  • @ABGR https://i.gyazo.com/81285e4f111dd466b670117ecee5565f.png here is what's inside `articolo.varianti` AND `obj.varianti` in this case it's the same.. or even by testing with `articolo.varianti` as empty array so `articoli.varianti == []` return -1 – NiceToMytyuk Jun 08 '20 at 08:59
  • @trincot actually in the inspect i have the `obj.varianti` as array of object of Varianti with 2 elements and even `articolo.varianti` has same array of object inside it.. check the screen i linked in the previous comment which include the values of `articolo.varianti` and here is even the inspection of `obj.varianti` https://i.gyazo.com/250bfc6372b1a0d5565002dfecb6d015.png – NiceToMytyuk Jun 08 '20 at 09:01
  • if `articolo.varianti` is something like this, `[{id: 1, name:'sdsd'}, {id: 2, name: 'sfasfaf'}].findIndex(i=>i.id === 2)`; it'd definitely produce a result – ABGR Jun 08 '20 at 09:08
  • 1
    `==` always returns false when the operands are two distinct arrays. You need to compare each element in the arrays. – trincot Jun 08 '20 at 09:09
  • @trincot so if arrays has the same elements but are distrinct `==` returns false? – NiceToMytyuk Jun 08 '20 at 09:11
  • Yes, indeed. You can try [1] == [1] is not true. – trincot Jun 08 '20 at 09:11
  • 1
    correct, by default ``array1 == array2 //true`` only when the variables point to the same memory address (i.e. same object on the heap). By default, JavaScript compares memory addresses of objects. – Alex L Jun 08 '20 at 09:12
  • 1
    @trincot definitely got it i'd compare each element at this point, thank you! – NiceToMytyuk Jun 08 '20 at 09:13

1 Answers1

1

It looks like your id or obj.varianti is undefined. You can use the folliwing example to find an index:

class Articoli {
  constructor(id, titolo, qta, prezzo, prezzo_s, img, varianti) {
      this.id = id;
      this.titolo = titolo;
      this.qta = qta;
      this.prezzo = prezzo;
      this.prezzo_s = prezzo_s;
      this.img = img;
      this.varianti = varianti;
  }
}


let articolies = [
  new Articoli(1, 'title 1', 'qta 1', 'prezzo1', 'prezzo_s 1', 'img 1', 'varianti 1'),
  new Articoli(1, 'title 111', 'qta 1', 'prezzo1', 'prezzo_s 1', 'img 1', 'varianti 111'),
  new Articoli(1, 'title 2', 'qta 2', 'prezzo2', 'prezzo_s 2', 'img 2', 'varianti 2')
]

let objToFind = new Articoli(1, 'title 111', 'qta 1', 'prezzo1', 'prezzo_s 1', 'img 1', 'varianti 111');
var objIndex = articolies.findIndex((obj => obj.id == objToFind.id && 
    obj.varianti == objToFind.varianti))

console.log(objIndex);
StepUp
  • 36,391
  • 15
  • 88
  • 148