Filtering Object Arrays

suggest change

The filter() method accepts a test function, and returns a new array containing only the elements of the original array that pass the test provided.

// Suppose we want to get all odd number in an array:
var numbers = [5, 32, 43, 4];
var odd = numbers.filter(function(n) {
  return n % 2 !== 0;
});
let odd = numbers.filter(n => n % 2 !== 0); // can be shortened to (n => n % 2)

odd would contain the following array: [5, 43].

It also works on an array of objects:

var people = [{
  id: 1,
  name: "John",
  age: 28
}, {
  id: 2,
  name: "Jane",
  age: 31
}, {
  id: 3,
  name: "Peter",
  age: 55
}];
var young = people.filter(function(person) {
  return person.age < 35;
});
let young = people.filter(person => person.age < 35);

young would contain the following array:

[{
  id: 1,
  name: "John",
  age: 28
}, {
  id: 2,
  name: "Jane",
  age: 31
}]

You can search in the whole array for a value like this:

var young = people.filter((obj) => {
  var flag = false;
  Object.values(obj).forEach((val) => {
    if(String(val).indexOf("J") > -1) {
      flag = true;
      return;
    }    
  });
  if(flag) return obj;
});

This returns:

[{
  id: 1,
  name: "John",
  age: 28
},{
  id: 2,
  name: "Jane",
  age: 31
}]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Arrays:
* Syntax
* Filtering Object Arrays

Table Of Contents
11 Arrays
12 Objects
14 Classes
16 Map
17 Set
24 Loops
27 Date
29 Scope
30 AJAX
35 Cookies
41 JSON
44 Fetch
45 Modules
46 Screen
64 Console
68 Symbols
73 Modals
76 Events
86 Proxy
89 WeakMap
90 WeakSet
102 Tilde