Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
ES6 - Remove object from array based on conditions and return rest of the array

There are three flags xstatus, ystatus and zstatus which determines whether the object with the properties name.is_xstatus, name.is_ystatus and name.is_zstatus should be present in the array or not.

//All flags are true
var xstatus = true;
var ystatus = true;
var zstatus = true;

    var myArray = [
        {field: 'id', name:'is_xstatus', operator: 'eq', value: 'id'}, 
        {field: 'cStatus', name:'is_ystatus', operator: 'eq', value: 'cStatus'}, 
        {field: 'money', name:'is_zstatus', operator: 'eq', value: 'money'},
        {field: 'coverage', name:'is_coverage', operator: 'eq', value: 'coverage'},
        {field: 'nam', name:'is_nam', operator: 'eq', value: 'nam'}    
    ];

var xstatus = true;
var ystatus = true;
var zstatus = true;
var myArray = [
    {field: 'id', name:'is_xstatus', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', name:'is_ystatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', name:'is_zstatus', operator: 'eq', value: 'money'},
    {field: 'coverage', name:'is_coverage', operator: 'eq', value: 'coverage'},
    {field: 'nam', name:'is_nam', operator: 'eq', value: 'nam'}
    
];

myArray = myArray.filter(( obj ) =>(!zstatus && obj.name !== 'is_zstatus')).filter((obj) => (!ystatus && obj.name !== 'is_ystatus'))

console.log(myArray);

I tried the above with the filter method. When the filter conditions don’t meet, it returns empty []. But what I need is if conditions aren’t met, return the Array with out removing any object

var xstatus = true;
var ystatus = true;
var zstatus = false;
var myArray = [
    {field: 'id', name:'is_xstatus', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', name:'is_ystatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', name:'is_zstatus', operator: 'eq', value: 'money'},
    {field: 'coverage', name:'is_coverage', operator: 'eq', value: 'coverage'},
    {field: 'nam', name:'is_nam', operator: 'eq', value: 'nam'}
];


myArray.forEach((obj, index) => {
  if(!zstatus && obj.name == 'is_zstatus') {
    delete myArray[index];
  }
  return myArray;
});

console.log(myArray);

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can't do this with chained calls to filter. The zstatus filter will remove all the elements with is_xtatus and is_ystatus, so they won't be available for those filters.

Instead, use a single call to filter() that tests all of the conditions using ||.

var xstatus = true;
var ystatus = true;
var zstatus = true;
var myArray = [
    {field: 'id', name:'is_xstatus', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', name:'is_ystatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', name:'is_zstatus', operator: 'eq', value: 'money'},
    {field: 'coverage', name:'is_coverage', operator: 'eq', value: 'coverage'},
    {field: 'nam', name:'is_nam', operator: 'eq', value: 'nam'}
];

myArray = myArray.filter((obj) =>
  (zstatus && obj.name == 'is_zstatus') ||
  (ystatus && obj.name == 'is_ystatus') ||
  (xstatus && obj.name == 'is_xtatus') ||
  (obj.name !== 'is_ystatus' && obj.name !== 'is_xstatus' && obj.name !== 'is_zstatus'));

console.log(myArray);

about 4 years ago · Juan Pablo Isaza Report

0

Another approach with the use of spread operator

var xstatus = false;
var ystatus = true;
var zstatus = true;
var myArray = [
    {field: 'id', name:'is_xstatus', operator: 'eq', value: 'id'}, 
    {field: 'cStatus', name:'is_ystatus', operator: 'eq', value: 'cStatus'}, 
    {field: 'money', name:'is_zstatus', operator: 'eq', value: 'money'},
    {field: 'coverage', name:'is_coverage', operator: 'eq', value: 'coverage'},
    {field: 'nam', name:'is_nam', operator: 'eq', value: 'nam'}
    
];

let arr1 = myArray.filter(( obj ) =>(zstatus && obj.name == 'is_zstatus'));
let arr2 = myArray.filter(( obj ) =>(xstatus && obj.name == 'is_xstatus'));
let arr3 = myArray.filter(( obj ) =>(ystatus && obj.name == 'is_ystatus'));
let remainingElements = myArray.filter((obj) => (obj.name !== 'is_ystatus' && obj.name !== 'is_xstatus' && obj.name !== 'is_zstatus'));
myArray = [ ...arr1, ...arr2, ...arr3, ...remainingElements];
console.log(myArray);

about 4 years ago · Juan Pablo Isaza Report

0

why not add a judgement after filter?

let temp = myArray.filter(( obj ) =>(!zstatus && obj.name !== 'is_zstatus')).filter((obj) => (!ystatus && obj.name !== 'is_ystatus'));
myArray = temp.length ? temp : myArray;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!