Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
In javascript find () method of an array how to get any element using only array argument of the callback function? See the code below
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?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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
about 4 years ago · Juan Pablo Isaza Denunciar

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar

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);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda