0

I have an array of object, I want to remove entire object from the array whose value is 1 using splice.I have an array of object, I want to remove entire object from the array whose value is 1 using splice. Here is the code:

 <!DOCTYPE html>
<html>

<body>
  <script>
    const CellDisplay = item => {
      switch (item) {
        case 'item1':
          return 'one';
        case 'item2':
          return 'two';
        case 'item3':
          return 'three';
        case 'item4':
          return 'four';
        case 'item5':
          return 'five';
      }
    };
    const forLaunch = lData => {
      for (const key in lData) {
        console.log(key);
        return CellDisplay(key);
      }
    }

    const Obj = [{
      Id: 575,
      items: {
        item1: '2020-12-08T10:00:00.000+0000',
        item2: '2020-11-12T00:00:00.000+0000',
        item3: '2020-12-08T10:00:00.000+0000',
        item4: null,
        item5: '2020-12-08T10:00:00.000+0000'
      },
      active: false
    }];
    Obj.forEach(data => {
      forLaunch(data.items);
    });
  </script>

</html>
  • 1
    Why using `splice` rather than using `filter` as you have in the qusetion's code? Your `filter` code isn't working because `filter` creates and returns a **new** array, but you're not using the array it returns. You *can* do this with `splice` instead, by [finding the object](https://stackoverflow.com/questions/18131434/how-to-find-an-appropriate-object-in-array-by-one-of-its-properties) using [`findIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) and then using that index with `splice`, but `filter` is idiomatic for this. – T.J. Crowder Oct 30 '20 at 10:06
  • See https://stackoverflow.com/questions/5767325/how-can-i-remove-a-specific-item-from-an-array – Zahra Bayat Oct 30 '20 at 10:08

0 Answers0