get object in array javascript code example

Example 1: javascript object to array

//ES6 Object to Array

const numbers = {
  one: 1,
  two: 2,
};

console.log(Object.values(numbers));
// [ 1, 2 ]

console.log(Object.entries(numbers));
// [ ['one', 1], ['two', 2] ]

Example 2: javascript find

const inventory = [
  {name: 'apples', quantity: 2},
  {name: 'cherries', quantity: 8}
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
  {name: 'cherries', quantity: 15}
  
];

const result = inventory.find( ({ name }) => name === 'cherries' );

console.log(result) // { name: 'cherries', quantity: 5 }

Example 3: javascript find object by property in array

// To find a specific object in an array of objects
myObj = myArrayOfObjects.find(obj => obj.prop === 'something');

Example 4: javascript find object in array

myArray.findIndex(x => x.id === '45');

Example 5: 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 + "<br>"); // 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 6: javascript find object array

const found = accesses.find(x => x.Resource === 'Clients');
console.log(found)