filter array of objects based on id javascript code example
Example 1: js filter array of objects by value
var heroes = [
{name: “Batman”, franchise: “DC”},
{name: “Ironman”, franchise: “Marvel”},
{name: “Thor”, franchise: “Marvel”},
{name: “Superman”, franchise: “DC”}
];
var marvelHeroes = heroes.filter(function(hero) {
return hero.franchise == “Marvel”;
});
Example 2: javascript filter array of objects by id
const myArray = [{id: 1, name:'pipi'}, {id: 2, name:'popo'}];
const id = 2;
const variableOne = myArray.filter(itemInArray => itemInArray.id === id);
console.log(cariableOne[0].name);