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);
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);
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);
find the index of an object in an array
const letters = [{letter: 'a'},{letter: 'b'},{letter: 'c'}]
const index = letters.findIndex((element, index) => {
if (element.letter === 'b') {
return true
}
})
//index is `1`
findindex js
// findIndex(callback fn)
// .... return index (when condition meets)
// .... return -1 (if condition not meets)
const array = [5, 12, 8, 130, 44];
/// it returns the index of number which satisfy the condition true
const index = array.findIndex((item)=> item>10); //1
/// now we can check what element at that index...
console.log(array[index]); // array[1]
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us