Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
¿Por qué el método Array.some() se comporta de manera diferente?

Tengo una matriz que contiene elementos vacíos.

 let arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5'];

Estoy tratando de averiguar si hay algún elemento vacío en una matriz usando el método Array.some() pero devuelve falso.

 // Input array const arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5']; // If you see in console, empty element has been filled with undefined console.log(arr); // ["item 1","item 2",undefined,"item 3",undefined,"item 4","item 5"] // Now I am just checking if is there any falsy value in an array. Ideally it should return true as we have undefined values in an arr but it is returning `false`. console.log(arr.some(item => !item)); // false

Hay muchas otras formas de verificar si hay valores undefined en una matriz, pero aquí pregunto específicamente sobre el comportamiento de Array.some() . Si voy a reemplazar explícitamente los elementos vacíos con undefined, devolverá true .

 const arr = ['item 1', 'item 2', undefined, 'item 3', undefined, 'item 4', 'item 5']; console.log(arr.some(item => !item)); // true

¿Alguien puede saber por qué se está comportando así? No proporcione ninguna solución alternativa, ya que conozco otras soluciones alternativas.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Ver MDN :

El método some() ejecuta la función callbackFn una vez para cada elemento presente en la matriz hasta que encuentra aquel en el que callbackFn devuelve un valor verdadero (un valor que se vuelve verdadero cuando se convierte en un valor booleano). Si se encuentra dicho elemento, some() inmediatamente devuelve verdadero. De lo contrario, some() devuelve falso. callbackFn se invoca solo para índices de la matriz con valores asignados. No se invoca para índices que han sido eliminados o a los que nunca se les han asignado valores.

Se puede ver que aquí:

 const arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5']; console.log(arr.some((item, index) => { console.log(item, index); return !item }));


Si voy a reemplazar explícitamente los elementos vacíos con indefinidos, entonces devolverá verdadero.

Esa es la diferencia entre "nunca se le asignó un valor" y "se le asignó un valor undefined ".

about 4 years ago · Santiago Trujillo Report

0

Los elementos están realmente empty y not undefined

 const arr = ['item 1', 'item 2', , 'item 3', , 'item 4', 'item 5']; // If you see in console, empty element has been filled with undefined console.log(arr); VM146:4 (7) ['item 1', 'item 2', empty, 'item 3', empty, 'item 4', 'item 5']

Esa es la razón por la que some scripts se comportan de manera diferente. Este hilo hace un buen trabajo explicando cosas.

Verifique que la matriz tenga un elemento vacío o no

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!