How does find method works with the array?

Viewed 888

Suppose I have an array and I want to find first number in an array which is greater than 25. It checks for the condition i.e. whether the number is greater than 25 and should return true when the condition satisfies but it returns the value.Help me debug how it is working.

const numbers=[10,20,30,40,50]

function findNumJS(numbers,num){
  const output=numbers.find(function(n){
      return n>num
  })
  return output
}
console.log(findNumJS(numbers,25))

6 Answers

The find() method returns the value of the first element in an array that pass a test (provided as a function).

The find() method executes the function once for each element present in the array:

If it finds an array element where the function returns a true value, find() returns the value of that array element (and does not check the remaining values) Otherwise it returns undefined Note: find() does not execute the function for empty arrays.

Note: find() does not change the original array.

For more : https://www.w3schools.com/jsref/jsref_find.asp

Array​.prototype​.find()

The find() method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

If you want to return Boolean value then you can use Array​.prototype​.some()

const numbers=[10,20,30,40,50]

function findNumJS(numbers,num){
  const output=numbers.some(function(n){
      return n>num
  })
  return output
}
console.log(findNumJS(numbers,25))

The javascript Array.find(callback) the function returns the first item of the array, where the function returns true.

function isBigEnough(value) {
  return function(element, index, array) {
    return (element >= value);
  }
}
var filtered = numbers = [10, 20, 30, 40, 50].filter(isBigEnough(25));
console.log(filtered)

Actually find method go through each and every array item one by one and check the provided condition. If the provided condition is satisfied then the find method returns the matched item immediately.

const numbers=[10,20,30,40,50]
numbers.find(number => number > 25)

//In first iteration number -> 10 ; which is not greater than 25
//In second iteration number -> 20 ; which is not greater than 25
//In third iteration number -> 30 ; which is greater than 25, so the condition satisfied and it returns the value

if you find the first element in the arrays you can use .find

but you want to check is there any element that satisfy your condition. so you should use .some.

for your example you can change it like this:

const numbers=[10,20,30,40,50]

function findNumJS(numbers,num){
  const output=numbers.some(function(n){
      return n>num
  })
  return output
}
console.log(findNumJS(numbers,25))

you can decrease your code like this or just user .some:

const numbers=[10,20,30,40,50]

function findNumJS(numbers,num){
  return numbers.some(function(n){
      return n>num
  })
}
console.log(findNumJS(numbers,25))

for more information see this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

Related