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);
how to find the index of a value in an array in javascript
var list = ["apple","banana","orange"]
var index_of_apple = list.indexOf("apple") // 0
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);
javascript indexof
//indexOf getting index of array element, returns -1 if not found
var colors=["red","green","blue"];
var pos=colors.indexOf("blue");//2
//indexOf getting index of sub string, returns -1 if not found
var str = "We got a poop cleanup on isle 4.";
var strPos = str.indexOf("poop");//9
get index of element in array js
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
beasts.indexOf('bison'); //ouput: 1
// start from index 2
beasts.indexOf('bison', 2); //output: 4
beasts.indexOf('giraffe'); //output: -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