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

119
Views
¿Por qué una matriz vacía de N longitud en javascript no tiene índices?

Así que he creado una matriz vacía en Javascript

 const x = Array(5);

O

 const x = new Array(5);

Y si verifico la longitud, es 5 en ambos casos, pero cuando lo recorro usando .map , entonces no me dio los índices, es decir, 0,1,2,3,4

 x.map((el, i) => i);

Esto debería devolver [0,1,2,3,4]

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Porque la especificación requiere que cuando se crea una matriz así con el constructor Array, donde values es la lista de argumentos:

 a. Let len be values[0]. b. Let array be ! ArrayCreate(0, proto). c. If Type(len) is not Number, then i. Perform ! CreateDataPropertyOrThrow(array, "0", len). ii. Let intLen be 1𝔽. d. Else, i. Let intLen be ! ToUint32(len). ii. If SameValueZero(intLen, len) is false, throw a RangeError exception. e. Perform ! Set(array, "length", intLen, true). f. Return array.

Eso es todo lo que hace, y nada más. En resumen, crea un nuevo objeto de matriz heredado de Array.prototype y luego establece una propiedad de length en él. No crea ningún índice numérico en la matriz.

Es como

 const createArray = (length) => { const newArr = Object.create(Array.prototype); newArr.length = length; return newArr; }; const arr = createArray(5); console.log(arr.length); console.log(arr.hasOwnProperty('0'));

Como resultado, si desea iterar sobre la matriz, primero deberá llenarla de alguna manera, ya sea con .fill , Array.from o extendiéndola en una nueva matriz. .

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