Q:

js get array item by property

var result = jsObjects.find(obj => {
  return obj.b === 6
})
2
const inventory = [
  {name: 'apples', quantity: 2},
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
];

function isCherries(fruit) { 
  return fruit.name === 'cherries';
}

console.log(inventory.find(isCherries)); 
// { name: 'cherries', quantity: 5 }
0
const jsObjects = [
  {id: 1, displayName: "First"}, 
  {id: 2, displayName: "Second"}, 
  {id: 3, displayName: "Third"}, 
  {id: 4, displayName: "Fourth"}
]

// You can use the arrow function expression:
var result = jsObjects.find(obj => {
	// Returns the object where
	// the given property has some value 
  	return obj.id === 1
})

console.log(result)

// Output: {id: 1, displayName: "First"}
0

New to Communities?

Join the community