1
var check = [1,2,3,4,5,6]; 
var check1 =check.find((elements,index,array)=>{ return array ;}); console.log(check1); 

Output is 1. Why? And if i want 3 as output from above array by giving checks or condition to the array argument then how to get it?

7
  • can you show us some code of what you are trying to accomplish? If you are just wanting to search the array for the value 3 then decho's answer shows that Commented Nov 13, 2021 at 17:04
  • Actually i am trying to get 3 by providing condition to that array argument only similarly as we give condition to element and index argument as for eg: element>1... Commented Nov 13, 2021 at 17:10
  • Why would you need to do that? Commented Nov 13, 2021 at 17:15
  • I just want to understand the concept of find method fully. Commented Nov 13, 2021 at 17:17
  • The concept of .find is to search through the array and return the first value that satisfies a given condition. That array parameter is not used for finding a value and returning it. If you wanted to change a value in the original array based on a condition then you can use that third param. Commented Nov 13, 2021 at 17:25

3 Answers 3

2

The find method executes the callbackFn function once for each index of the array until the callbackFn returns a truthy value. If so, find immediately returns the value of that element. Otherwise, find returns undefined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

And since you aren't really doing any checks and just returning c (a truthy value), which is the array itself, that's why it returns 1.

If you want to find 3 or any other value, you do the following (also name your function params better so it's easier to understand what is going on):

var check = [1, 2, 3, 4, 5, 6];
var check3 = check.find((element, index, array) => {
  return element === 3
});
console.log(check3); // output : 3
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for answering and for the feedback too. Plz tell me one more thing that how to give checks or contion to that array argument only to get output 3??
@PrakharAgarwal I am sorry, I am not sure I understand your question exactly. What is the end result that you are trying to accomplish?
1

In response to OP request in comments. The third parameter of the callback function passed into array.find can be used to modify the original array based on a condition. This example switches even values to the odd value 1.

const check = [1,2,3,4,5,6]
check.find((el, index, arr) => {
    if (el % 2 === 0){
        arr[index] = 1
    }
})
console.log(check) // [2,2,3,4,5,6]

However, this is not the proper use of .find, and .find should not be used in this way. .find should be used to simply return the first value of an array that satisfies a condition, as decho explained in his answer. If you are wanting to update the values in an array consider using a for loop or .forEach.

1 Comment

Thank you so much dear .. its really helpfull
0

Based on your comment here's how to check if a number exists at a particular index, and returning the number based on that check.

var check = [1, 2, 3, 4, 5, 6];

var check1 = check.find((el, i, arr) => {
  return arr.indexOf(3) > 1 && el === 3;
});

var check2 = check.find((el, i, arr) => {
  return arr.indexOf(3) > 3 && el === 3;
});

console.log(check1);
console.log(check2);

1 Comment

Thank you so much. It really helped me...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.