0

I have this array of object in javascript.

var array = [ { DATA_ID: 1,
    DATA_NAME: 'XX',
    DATA_GROUP: 2313,
    CODE_NO: 1 },
  { DATA_ID: 6,
    DATA_NAME: 'YY',
    DATA_GROUP: 4213,
    CODE_NO: 2 },
  { DATA_ID: 4,
    DATA_NAME: 'ZZ',
    DATA_GROUP: 2123,
    CODE_NO: 3 },
  { DATA_ID: 8,
    DATA_NAME: 'ZZ',
    DATA_GROUP: 2123,
    CODE_NO: 3 } ]

I want to remove objects from the array of objects when the property CODE_NO === 3.

The result will look like this;

[ { DATA_ID: 1,
    DATA_NAME: 'XX',
    DATA_GROUP: 2313,
    CODE_NO: 1 },
  { DATA_ID: 6,
    DATA_NAME: 'YY',
    DATA_GROUP: 4213,
    CODE_NO: 2 }
]

I am using node.js v6

2

4 Answers 4

1

You can do simple java script manipulation by array.filter :

var arr = [ { DATA_ID: 1,
    DATA_NAME: 'XX',
    DATA_GROUP: 2313,
    CODE_NO: 1 },
  { DATA_ID: 6,
    DATA_NAME: 'YY',
    DATA_GROUP: 4213,
    CODE_NO: 2 },
  { DATA_ID: 4,
    DATA_NAME: 'ZZ',
    DATA_GROUP: 2123,
    CODE_NO: 3 },
  { DATA_ID: 8,
    DATA_NAME: 'ZZ',
    DATA_GROUP: 2123,
    CODE_NO: 3 } ]

var res =arr.filter(function(elem){

  if (elem.CODE_NO != 3)
    return elem
})
console.log(res)

Sign up to request clarification or add additional context in comments.

1 Comment

Elegant code! I was writing some long-winded code until I saw your answer. Brilliant! Upvoted.
1

**use this : **

    let array = 
     [ { DATA_ID: 1,
        DATA_NAME: 'XX',
        DATA_GROUP: 2313,
        CODE_NO: 1 },
      { DATA_ID: 6,
        DATA_NAME: 'YY',
        DATA_GROUP: 4213,
        CODE_NO: 2 },
      { DATA_ID: 4,
        DATA_NAME: 'ZZ',
        DATA_GROUP: 2123,
        CODE_NO: 3 },
      { DATA_ID: 8,
        DATA_NAME: 'ZZ',
        DATA_GROUP: 2123,
        CODE_NO: 3 } ];

        for (var key in array ){
          if(array[key].CODE_NO==3){
            delete array[key];
          }
       }

Comments

1

I'll post an "alternative" solution if there's already Ramda project dependency. @asdf_enel_hak example is great purse JS so if you don't need extra deps, use that example.

Ramda based solution:

// complement is like logical not, if value is true returns false and on true returns false
// it does not return boolean value but new function
const codeIsNot3 = R.complement(
  R.propEq("CODE_NO", 3) // Property based equal check, without R.complement it would return all objects where code_no is 3
);
R.filter(codeIsNot3, arr);

2 Comments

Functional programming looks good. But I will need some time to understand the code. The code is short, yet not easy for people like me to understand.
That's true, added more comments about the functions used, also ramda docs is great place to go next.
0

This code will work. The trick lies in using array.splice(i,1) to remove the object in question.

for (let i=0;i< array.length; i++  ){
      if (array[key].CODE_NO===3){            
        array.splice(i,1);
      }
   }

array is the array posted in your question.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.