Search code examples
javascriptarraysobjectecmascript-6

Check if object has particular property


Let's say I have an array of objects

const arr = [ {name:"Bob", age: 20}, { name: "Sara", age: 22}, { name:
Tom, age:20} ];

I want to print objects with particular property, for example only those with the age == 20. So The result would be

const arr = [ {name:"Bob", age: 20}, { name: Tom, age:20} ];

I really want to do it with ES6. Do you have any suggestion what method could be used?


Solution

  • This will do

    var filteredData = arr.filter((e) => e.age === 20)