DekGenius.com
JAVASCRIPT
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`
Get the index of an Object in an Array in JavaScript
const Season = [
{
month:'January',
season: 'Winter',
},
{
month:'March',
season: 'Spring',
},
{
month:'June',
season: 'Summer',
},
{
month:'August',
season: 'Autumn',
},
]
const index = Season.findIndex( (loopVariable) => loopVariable.month === 'March');
console.log("The index of March Month is",index)
Get the index of an Object in an Array in JavaScript
const Season = [
{
month:'January',
season: 'Winter',
},
{
month:'March',
season: 'Spring',
},
{
month:'June',
season: 'Summer',
},
{
month:'August',
season: 'Autumn',
},
]
const index = Season.map( (loopVariable) => loopVariable.month).indexOf
("March"));
console.log("The index of March Month is",index)
get the index of object in array
var elementPos = array.map(function(x) {return x.id; }).indexOf(idYourAreLookingFor);
var objectFound = array[elementPos];
© 2022 Copyright:
DekGenius.com