-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.

Aztec-3x5
  • 145
  • 1
  • 11
  • `.filter()` returns a new array, not modifies it in place. And filter returns `true`/`false`. I suggest reading [the documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) on filter which has plenty of examples – Andy Ray Jun 22 '21 at 17:26

4 Answers4

0

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)))
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53
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)
Siva K V
  • 10,561
  • 2
  • 16
  • 29
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)));
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
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.

mukhtar alam
  • 195
  • 1
  • 12