javascript filter value in array of objects code example

Example 1: how to filter an array of objects in javascript

let arr=[{id:1,title:'A', status:true}, {id:3,title:'B',status:true}, {id:2, title:'xys', status:true}];
//find where title=B
let x = arr.filter((a)=>{if(a.title=='B'){return a}});
console.log(x)//[{id:3,title:'B',status:true}]

Example 2: filter out object in array using two arguments

var arr= [{id: "123", name: "Faa"},
          {id: "123", name: "Bar"},
          {id: "345", name: "Foo"},
          {id: "678", name: "FaaBar"}
         ];

var name = 'FaaBar';
var id = '678';

arr = arr.filter(function(elem) {
  //return false for the element that matches both the name and the id
  return !(elem.id == id && elem.name == name)
});

Example 3: javascript filter array of objects

function isBigEnough(value) {
  return value >= 10
}

let filtered = [12, 5, 8, 130, 44].filter(isBigEnough)
// filtered is [12, 130, 44]

Tags:

Misc Example