Search Objects on an array

JS
S
JavaScript

Snippet to find objects inside an array

1// Approach 1 (by index)
2myArray.findIndex(i => i.item === "workload"); // 1
3
4// Approach 2 (by key:value)
5function search(myArray) {
6    return myArray.item === 'workload';
7}
8myArray.find(search) // { item: 'chips', price: '3.45' }
9
10

Created on 7/11/2018