[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/find [Back]  [Original]

Array.prototype.find() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.find()

Baseline

20159

find() Array undefined

const array = [5, 12, 8, 130, 44];

const found = array.find((element) => element > 10);

console.log(found);
// : 12

js
find(callbackFn)
find(callbackFn, thisArg)

callbackFn

element

index

array

find()

thisArg

callbackFn this

undefined

find() callbackFn callbackFn find() callbackFn find() undefined

callbackFn undefined

find() this length

js
const inventory = [
  { name: "apples", quantity: 2 },
  { name: "bananas", quantity: 0 },
  { name: "cherries", quantity: 5 },
];

function isCherries(fruit) {
  return fruit.name === "cherries";
}

console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }

js
const inventory = [
  { name: "apples", quantity: 2 },
  { name: "bananas", quantity: 0 },
  { name: "cherries", quantity: 5 },
];

const result = inventory.find(({ name }) => name === "cherries");

console.log(result); // { name: 'cherries', quantity: 5 }

undefined

js
function isPrime(n) {
  if (n < 2) {
    return false;
  }
  if (n % 2 === 0) {
    return n === 2;
  }
  for (let factor = 3; factor * factor <= n; factor += 2) {
    if (n % factor === 0) {
      return false;
    }
  }
  return true;
}

console.log([4, 6, 8, 12].find(isPrime)); // undefined, 
console.log([4, 5, 8, 12].find(isPrime)); // 5

: isPrime()

callbackFn 3

array filter() find()

js
const numbers = [3, -1, 1, 4, 1, 5, 9, 2, 6];
const firstTrough = numbers
  .filter((num) => num > 0)
  .find((num, idx, arr) => {
    // arr 
    // 
    if (idx > 0 && num >= arr[idx - 1]) return false;
    if (idx < arr.length - 1 && num >= arr[idx + 1]) return false;
    return true;
  });
console.log(firstTrough); // 1

find()

undefined

js
//  2, 3, 4 
const array = [0, 1, , , , 5, 6];

// 
array.find((value, index) => {
  console.log("Visited index", index, "with value", value);
  return false;
});
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value 5
// Visited index 6 with value 6

// 
array.find((value, index) => {
  //  5 
  if (index === 0) {
    console.log("Deleting array[5] with value", array[5]);
    delete array[5];
  }
  //  5 
  console.log("Visited index", index, "with value", value);
  return false;
});
// Deleting array[5] with value 5
// Visited index 0 with value 0
// Visited index 1 with value 1
// Visited index 2 with value undefined
// Visited index 3 with value undefined
// Visited index 4 with value undefined
// Visited index 5 with value undefined
// Visited index 6 with value 6

find()

find() this length length

js
const arrayLike = {
  length: 3,
  "-1": 0.1, // -1 < 0  find() 
  0: 2,
  1: 7.3,
  2: 4,
};
console.log(Array.prototype.find.call(arrayLike, (x) => !Number.isInteger(x)));
// 7.3

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.find


Web Proxy Viewer  |  New URL  |  Original Page