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

114
Views
cómo hacer una matriz de objetos a partir de varias matrices

Por ejemplo,

Tengo un total de dos arreglos.

La matriz de claves tiene claves.

Las matrices restantes son matrices múltiples en una matriz como [[A], [B], [C], ...]

Entonces, ¿cómo ponemos objetos en una matriz?

 let key = [“name”, “age”, “country”]; let values = [["james", "23", "US"],["steve", "20", "UK"], ...] ** expect result ** result = [ { "name": "james", "age": 23, "country": "US" }, { "name": "steve", "age": 20, "country": "UK" }, ... ]
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

si tiene datos como la edad del nombre y el país con las mismas claves para que pueda probar el código a continuación, debería funcionar para usted.

 let values = [["james", "23", "US"],["steve", "20", "UK"], ...] const result = []; values.forEach(item => { const [name, age, country] = item; result.push({ name, age, country }); }); console.log(result)
about 4 years ago · Santiago Trujillo Report

0

Una sola línea usando el map de matriz y reduce funciones:

 values.map(v=>(key.reduce((acc, k, i) => ({...acc, [k]: v[i]}), {})));

(Tenga en cuenta que su pregunta original contiene comillas “” que no son ASCII; debe usar " para una sintaxis JS válida).

about 4 years ago · Santiago Trujillo Report

0

Acabo de hacer su pregunta a OpenAI JavaScript Codex y devolvió este resultado.

 /* let key = [“name”, “age”, “country”]; */ var key = ["name", "age", "country"]; /* let values = [["james", "23", "US"],["steve", "20", "UK"]] */ var values = [["james", "23", "US"],["steve", "20", "UK"]]; /* So how do we put objects in one array? */ var objects = []; for (var i = 0; i < values.length; i++) { var object = {}; for (var j = 0; j < key.length; j++) { object[key[j]] = values[i][j]; } objects.push(object); }

Realmente no sé si funciona o no, pero tengo curiosidad por ver qué hace. Deberías probar esto.

Por favor, perdóname si no funciona. Gracias

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!