filter results of object javascript code example
Example 1: how to filter object in javascript
// use filter() function. Let say we have array of object.
let arr = [
{name: "John", age: 30},
{name: "Grin", age: 10},
{name: "Marie", age: 50},
];
//Now I want an array in which person having age more than 40 is not
//there, i.e, I want to filter it out using age property. So
let newArr = arr.filter((person)=>(return person.age <= 40));
//filter function expects a function if it return true then it added
//into new array, otherwise it is ignored. So new Array would be
/* [
{name: "John", age: 30},
{name: "Grin", age: 10},
]
*/
Example 2: javascript filter array of objects by array
var arr = [1,2,3,4],
brr = [2,4],
res = arr.filter(f => !brr.includes(f));
console.log(res);