Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Find object by any property

// Sample array
var myArray = [
    {"id": 1, "name": "Alice"},
    {"id": 2, "name": "Peter"},
    {"id": 3, "name": "Harry"}
];

// Get the Array item which matchs the id "2"
var result = myArray.find(item => item.id === 2);
console.log(result.name);  // Prints: Peter
// Get the index of Array item which matchs the id "2"
var index = myArray.findIndex(item => item.id === 2);
console.log(index);  // Prints: 1
console.log(myArray[index].name);  // Prints: Peter
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #Find #object #property
ADD COMMENT
Topic
Name
3+2 =