-1

l want to push object array into the main array. l want to check if that object array is already exist don't push . My code below he is pushing objects although are already exists.

 add_addon(addon, title, index) {
    if (this.items_local[index].addons) {

      for (let i = 0; i < this.items_local[index].addons.length; i++) {

        const element = this.items_local[index].addons[i];
        if (element.title === addon.title) {

          console.log(element.title === addon.title);
          alert('item is exist');
        } else {
          this.items_local[index].addons.push(addon);
        }
      }
    }
  }

output :

[
  {
    "size": true,
    "desc": "صحن مخلمة",
    "ratting": 0,
    "quantiy": 1,
    "name": "مخلمة",
    "id": "QmzeJgg2F2",
    "price": 500,
    "addons": [
      {
        "title": "لحم بعجين ",
        "price": 2
      },
      {
        "title": "خبز بعجين",
        "price": 0.1
      },
      {
        "title": "لحم بعجين ",
        "price": 2
      },
      {
        "title": "خبز بعجين",
        "price": 0.1
      },
      {
        "title": "خبز بعجين",
        "price": 0.1
      }
    ]
  }
]

How to prevent pushing objects array if already exist?

  • 3
    Because as soon as you have an element that *isn't* the one you're trying to add you add it. Simple debugging via logging would show *immediately* what is happening in your code--you don't debug *enough* here because you only log something when the element title matches, thus completely miss all the times it *doesn't* match. – Dave Newton Jun 16 '21 at 17:07
  • @DaveNewton , So what l have to do ? – Sky conditions Jun 16 '21 at 17:11
  • @DaveNewton l have added the output to the question – Sky conditions Jun 16 '21 at 17:14
  • Only add it if *no* titles match, not just when a *single* title doesn't match. – Dave Newton Jun 16 '21 at 17:25

1 Answers1

1

You can use some to check if the item exists, instead of iterating through the array manually:

   // ...
   if (!this.items_local[index].addons.some(e => e.title === addon.title)) {
     this.items_local[index].addons.push(addon);
   }
   // ...

If you really insist on iterating manually, you should be checking if any iteration returned positive, not just the current iteration. In other words:

  // ...
  let addonExists = false;
  for (let i = 0; i < this.items_local[index].addons.length; i++) {

    const element = this.items_local[index].addons[i];
    if (element.title === addon.title) {

      addonExists = true;
      alert('item is exist');
      break;
    }
  }
  addonExists || this.items_local[index].addons.push(addon);
  // ...
Guest
  • 182
  • 1
  • 8
  • l did that before , but l dont know why he doesnt work . Now he is working fine . Thanks – Sky conditions Jun 16 '21 at 17:24
  • What if l want to delete specific object array ! . l mean how can l access to that object array index key to remove it from the array ? @Guest – Sky conditions Jun 16 '21 at 18:06
  • @Skyconditions If you want to delete elements by `title`, you can use [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). Alternatively, if you want to delete no more than one element at a time, you can use [`findIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) to locate it and then then [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) to remove it. However, since `title` is unique, `filter` should work fine. – Guest Jun 16 '21 at 18:18
  • l am very appreciate your help. Thank you – Sky conditions Jun 16 '21 at 18:20
  • @Skyconditions Furthermore, if possible, I would recommend using [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) instead of `Array`, using each element's `title` as its key. This would remove the need for redundancy checking, and also would make it easier to delete elements. – Guest Jun 16 '21 at 18:20
  • l will , Thank you – Sky conditions Jun 16 '21 at 18:48