Como encontrar o índice de um valor em uma matriz em JavaScript
var list = ["apple","banana","orange"]
var index_of_apple = list.indexOf("apple") // 0
Clumsy Crayfish
var list = ["apple","banana","orange"]
var index_of_apple = list.indexOf("apple") // 0
//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
a = [
{prop1:"abc",prop2:"qwe"},
{prop1:"bnmb",prop2:"yutu"},
{prop1:"zxvz",prop2:"qwrq"}
];
index = a.findIndex(x => x.prop2 ==="yutu");
console.log(index);
/*To search for the index of a substring or string within an array,
use .indexOf()
If you search for something that isn't there, the function will return -1*/
let mechs = ["madcat", "blood asp", "atlas"];
console.log(mechs.indexOf("madcat"); //returns 0
console.log(mechs.indexOf("atlas"); //returns 2
let clan = "nova cat";
console.log(clan.indexOf("a")); /*returns 3 (Only the first instance of the
character is used.*/
console.log(clan.indexOf("cat")); /*returns 5 (Finds the index of the beginning
character)*/
console.log(clan.indexOf("s")); //returns -1
var array = [2, 9, 9];
array.indexOf(2); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0
const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];
const index = fruits.findIndex(fruit => fruit === "blueberries");
console.log(index); // 3
console.log(fruits[index]); // blueberries