“Indexof JS” Réponses codées

Comment trouver l'index d'une valeur dans un tableau en javascript

var list = ["apple","banana","orange"]

var index_of_apple = list.indexOf("apple") // 0
Clumsy Crayfish

Index JavaScript

//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
Grepper

Index javascript avec condition

a = [
  {prop1:"abc",prop2:"qwe"},
  {prop1:"bnmb",prop2:"yutu"},
  {prop1:"zxvz",prop2:"qwrq"}
];
    
index = a.findIndex(x => x.prop2 ==="yutu");

console.log(index);
Clever Crane

Indice de

/*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
Faithful Fox

Index de tableau Javasript

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
Cookie_Wookie_7

Indexof JS

const fruits = ["apple", "banana", "cantaloupe", "blueberries", "grapefruit"];

const index = fruits.findIndex(fruit => fruit === "blueberries");

console.log(index); // 3
console.log(fruits[index]); // blueberries
Breakable Bug

Réponses similaires à “Indexof JS”

Questions similaires à “Indexof JS”

Plus de réponses similaires à “Indexof JS” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code