| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf | [Back] [Original] |
Get to know MDN better
const animals = ["Dodo", "Tiger", "Penguin", "Dodo"];
console.log(animals.lastIndexOf("Dodo"));
// : 3
console.log(animals.lastIndexOf("Tiger"));
// : 1
lastIndexOf(searchElement)
lastIndexOf(searchElement, fromIndex)
searchElementfromIndex -array.length <= fromIndex < 0 fromIndex + array.length fromIndex < -array.length -1 searchElement fromIndex >= array.length fromIndex undefined array.length - 1 searchElement -1
lastIndexOf() searchElement === NaN searchElement NaN lastIndexOf() -1
lastIndexOf()
const numbers = [2, 5, 9, 2];
numbers.lastIndexOf(2); // 3
numbers.lastIndexOf(7); // -1
numbers.lastIndexOf(2, 3); // 3
numbers.lastIndexOf(2, 2); // 0
numbers.lastIndexOf(2, -2); // 0
numbers.lastIndexOf(2, -1); // 3
lastIndexOf() NaN
const array = [NaN];
array.lastIndexOf(NaN); // -1
lastIndexOf push
const indices = [];
const array = ["a", "b", "a", "c", "a", "d"];
const element = "a";
let idx = array.lastIndexOf(element);
while (idx !== -1) {
indices.push(idx);
idx = idx > 0 ? array.lastIndexOf(element, idx - 1) : -1;
}
console.log(indices);
// [4, 2, 0]
idx == 0
fromIndex
indexOf()
lastIndexOf()
console.log([1, , 3].lastIndexOf(undefined)); // -1
lastIndexOf() this length length
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 2,
3: 5, // length 3 lastIndexOf()
};
console.log(Array.prototype.lastIndexOf.call(arrayLike, 2));
// 2
console.log(Array.prototype.lastIndexOf.call(arrayLike, 5));
// -1
| ECMAScript 2027 LanguageSpecification # sec-array.prototype.lastindexof |
Arrayat()concat()copyWithin()entries()every()fill()filter()find()findIndex()findLast()findLastIndex()flat()flatMap()forEach()includes()indexOf()join()keys()lastIndexOf()map()pop()push()reduce()reduceRight()reverse()shift()slice()some()sort()splice()toLocaleString()toReversed()toSorted()toSpliced()toString()unshift()values()with()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |