-1

I hope you're having a great day.

This is pretty straight-forward. I have an object array which I want to filter out, with the help of another array. Scenario is illustrated below:

var ob_array = [
                  {'a' : 1, 'col_2' : 'abc'},
                  {'a' : 2, 'col_2' : 'xyz'},
                  {'a' : 3, 'col_2' : 'jkl'}
                 ];

var my_array = [1, 2];

Expected Output:

var my_array = [
                 {'a' : 3, 'col_2' : 'jkl'}
               ]

Tried Approaches:

#1:

ob_array.filter(function(i){
   my_array.forEach(function(q){
      i['a'] == q
   })
});

#2:

ob_array.filter(function(i){
   for(var j in my_array){
      i['a'] == j;
   }
});

I mostly tend to use filter function because I love it. Thanks for viewing this small query of mine. I appreciate your time and efforts.

1
  • .filter() returns a new array, not modifies it in place. And filter returns true/false. I suggest reading the documentation on filter which has plenty of examples Commented Jun 22, 2021 at 17:26

4 Answers 4

1

You could use Array.includes instead of iterating over my_array:

var ob_array = [{
    'a': 1,
    'col_2': 'abc'
  },
  {
    'a': 2,
    'col_2': 'xyz'
  },
  {
    'a': 3,
    'col_2': 'jkl'
  }
];

var my_array = [1, 2];

console.log(ob_array.filter(ob => !my_array.includes(ob.a)))

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

1 Comment

It really surprises that how simple and effective your approach is. Thank you mate.
0

Use filter and includes

var ob_array = [
                  {'a' : 1, 'col_2' : 'abc'},
                  {'a' : 2, 'col_2' : 'xyz'},
                  {'a' : 3, 'col_2' : 'jkl'}
                 ];

var my_array = [1, 2];

const res = ob_array.filter(({a}) => !my_array.includes(a));

console.log(res)

Comments

0

The Array#filter takes a predicate i.e. a callback that returns true or false. if true is returned the element is allowed and false otherwise.

You are not returning the boolean result from your predicate callback, your code can be modified as below to return false if the element with key a exists in my_array or true otherwise:

var ob_array = [{
    'a': 1,
    'col_2': 'abc'
  },
  {
    'a': 2,
    'col_2': 'xyz'
  },
  {
    'a': 3,
    'col_2': 'jkl'
  }
];

var my_array = [1, 2];

const res = ob_array.filter(function(i) {
  for (var j of my_array) {
    if (i['a'] === j) {
      return false;
    }
  }
  return true;
});

console.log(res);

But a more elegant approach exists with the help of Array#includes:

var ob_array = [{
    'a': 1,
    'col_2': 'abc'
  },
  {
    'a': 2,
    'col_2': 'xyz'
  },
  {
    'a': 3,
    'col_2': 'jkl'
  }
];

var my_array = [1, 2];

console.log(ob_array.filter(o => !my_array.includes(o.a)));

Comments

0
[{'a' : 1, 'col_2' : 'abc'},
              {'a' : 2, 'col_2' : 'xyz'},
              {'a' : 3, 'col_2' : 'jkl'}
             ].filter(function(i){return (i.a!=1 && i.a!=2)})

the above code gives the required output.

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.