Vue numerical object and object indexOf () problem

current function

const index1 = JSON.stringify(this.selectGame).indexOf(JSON.stringify(item1));
this.selectGameitem1

previous function

    //
    check_show(item1) {
      const index1 = this.selectGame.indexOf(item1);
      // let aa = { idName:item.Id,nameId:item.Name}
      if(index1 > -1 ){
        this.selectGame.splice(index1, 1)
      }else if(this.selectGame.length < 10){
        this.selectGame.push(item1);
      }
    },

the above function is in this.selectGame.push (item1); the element added in the execution is valid. If the selectGame already contains the element in the middle, it won"t work, so I use JSON.stringify to convert the item1 into a string, but the problem is that the result from indexOf is the length of the string, such as 317, index1, 175, 818, 2500, etc., you can"t use this.selectGame.splice (index1, 1) to delete the included functions by position

.
Aug.25,2021

item is an object, you may be better off using findIndex, JSON.strify is hardly used in this scenario.
if item has distinct properties, such as id, then the index to see whether the item is in the list can use let index = this.selectGame.findIndex (item = > item.id = item1.id), that is, to find the subscript of the same item of id and item1 in the list. If it does not exist, it is-1.


. I replied to you yesterday's question

clipboard.png

Menu