“Index JavaScript” 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

JavaScript FindIndex

const array1 = [5, 12, 8, 130, 44];
const search = element => element > 13;
console.log(array1.findIndex(search));
// expected output: 3

const array2 = [
  { id: 1, dev: false },
  { id: 2, dev: false },
  { id: 3, dev: true }
];
const search = obj => obj.dev === true;
console.log(array2.findIndex(search));
// expected output: 2
SmokeFrog

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

Réponses similaires à “Index JavaScript”

Questions similaires à “Index JavaScript”

Plus de réponses similaires à “Index JavaScript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code