Home > Blockchain >  filter out elements from array in javascript
filter out elements from array in javascript

Time:12-06

Below is the array

[
  {
    CODE: '1',
    TIME: '06-10-2022 11:07:58',
    STATUS: '1',

  },
  {
    CODE: '1',
    TIME: '06-10-2022 12:14:27',
    STATUS: '0',

  },
  {
    CODE: '1',
    TIME: '06-10-2022 12:14:27',
    STATUS: '1',

  },  
  {
    CODE: '3',
    TIME: '13-10-2022 08:36:05',
    STATUS: '0',
    
  },
  {
    CODE: '3',
    TIME: '13-10-2022 10:27:16',
    STATUS: '1',

  },
  {
    CODE: '12',
    TIME: '13-10-2022 08:36:05',
    STATUS: '1',
    
  },
  {
    CODE: '12',
    TIME: '13-10-2022 10:27:16',
    STATUS: '0',

  }
]

i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0
i want to filter out the status which starts with 1 OR ends with 0

Expected output:-

[
 
  {
    CODE: '1',
    TIME: '06-10-2022 12:14:27',
    STATUS: '0',

  },
  {
    CODE: '1',
    TIME: '06-10-2022 12:14:27',
    STATUS: '1',

  },  
  {
    CODE: '3',
    TIME: '13-10-2022 08:36:05',
    STATUS: '0',
    
  },
  {
    CODE: '3',
    TIME: '13-10-2022 10:27:16',
    STATUS: '1',

  },
  {
    CODE: '12',
    TIME: '13-10-2022 08:36:05',
    STATUS: '1',
    
  },

]
  

CodePudding user response:

Assuming this is what you want:

Take an array and return a new array with only elements that do not have a STATUS that either starts with 1 or ends with 0.

Try this:

function noOnesOrZeroes(array) {
  return array.filter(item => !/(^1|0$)/.test(item.STATUS));
}

CodePudding user response:

Your question and screenshots don't match up for me. I wrote this program that will output an element in the array if the STATUS is either 0 OR 1.

for (let i = 0; i < myArray.length; i  ) {

   if (myArray[i].STATUS == 0 || myArray[i].STATUS == 1) {
        console.log(myArray[i]);
   }

}

Replace "myArray" with your array identifier and it should work.

  • Related