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

187
Views
¿Cómo crear una matriz de objetos a través del mapa?

Me gustaría tener varias matrices de objetos como este.

P.ej:

 const pets = [ { name: "cat", age: 4 }, { name: "dog", age: 6 } ]

Pero quiero crearlo usando un mapa. Así que estaba intentando algo como esto.

 let pets = []; pets.map((item) => { return ( item.push({ name: "cat", age: 4 }, { name: "dog", age: 6 }) ) })

Con este método, obtengo una matriz vacía.

Entonces, suponiendo que esto sea incorrecto, ¿cómo continuaría y haría esto a través de un mapa?

Por favor, cualquier ayuda sería apreciada.

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

0

en primer lugar, el mapa funciona recorriendo una matriz, pero tiene una matriz vacía let pets = []; ¡así que el ciclo ni siquiera comienza! es por eso que obtienes una matriz vacía

En segundo lugar, el mapa es esencialmente un método a través del cual podemos crear una nueva matriz con la ayuda de una matriz existente, ¡así que ha elegido una forma incorrecta!

ejemplo de mapa

 const fruits = ["Mango", "Apple", "Banana", "Pineapple", "Orange"]; console.log(fruits); const uppercaseFruits = fruits.map((fruit)=>{ return fruit.toUpperCase(); // this thing will be added to new array in every iteration }); console.log(uppercaseFruits);

pero aún ....

 let pets = [""]; // an item so that loop can start const myPets = pets.map((item) => { return ( ([{ name: "cat", age: 4 },{ name: "dog", age: 6 }]) ) }) console.log(myPets)

about 4 years ago · Juan Pablo Isaza Report

0

 //Usage of map: for example let array = [1, 2, 3, 4, 5]; let newArray = array.map((item) => { return item * item; }) console.log(newArray) // [1, 4, 9, 16, 25]

map no cambiará la matriz original, si no le asigna un valor, la matriz original nunca se verá afectada

Y si quieres obtener lo que quieres, usa RANDOM así

 //random String function randomString(e) { e = e || 32; var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678", a = t.length, n = ""; for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a)); return n }

 //random Number function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); return(Min + Math.round(Rand * Range)); } var num = GetRandomNum(10000,999999); alert(num);

Luego puede combinar cadenas aleatorias y números aleatorios en un nuevo Objeto a través de una función

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!