Answers for "js array indexof object"

9

javascript indexOf object value in array

// Get index of object with specific value in array
const needle = 3; // needle
const haystack = [{ id: 1 }, { id: 2 }, { id: 3 }]; // haystack
const index = haystack.findIndex(item => item.id === needle);
Posted by: Guest on September-23-2020
4

javascript get index of object in array

// Get index of object with specific value in array
const needle = 3;
const haystack = [{ id: 1 }, { id: 2 }, { id: 3 }];
const index = haystack.findIndex(item => item.id === needle);
Posted by: Guest on September-23-2020
2

index of value in array

// for dictionary case use findIndex 
var imageList = [
   {value: 100},
   {value: 200},
   {value: 300},
   {value: 400},
   {value: 500}
];
var index = imageList.findIndex(img => img.value === 200);
Posted by: Guest on June-27-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language