javascript get object where value code example

Example 1: javascript access property values list of objects

// Access all properties and values in a JS object:
let valuesArray = Object.entries(MyObject);
  
   for (let value of valuesArray) { 
       document.write(value + "
"); // value is the property,value pair } /* Result: propName,value propName,value ... For clarity: */ let person = { name: "Piet", age: 42 }; Object.keys(person) // = ["name", "age"] Object.values(person) // = ["Piet", 42] Object.entries(person) // = [ ["name","Piet"], ["age",42] ]

Example 2: search an array of objects with specific object property value

// MDN Ref:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

var result = jsObjects.find(obj => {
  return obj.b === 6
});

Example 3: javascript get object where

myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]

myArray.find(x => x.id === '45').foo;

Tags:

Misc Example