filter out only one object from array javascript code example
Example 1: 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 !(elem.id == id && elem.name == name)
});
Example 2: filter array by keyword
var data = [
{email: "[email protected]",nama:"User A", Level:"Super Admin"},
{email: "[email protected]",nama:"User B", Level:"Super Admin"},
{email: "[email protected]",nama:"User C", Level:"Standart"},
{email: "[email protected]",nama:"User D", Level:"Standart"},
{email: "[email protected]",nama:"User E", Level:"Admin"},
{email: "[email protected]",nama:"User F", Level:"Standart"}
];
var filter = "Level";
var keyword = "Standart";
var filteredData = data.filter(function(obj) {
return obj[filter] === keyword;
});
console.log(filteredData);