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

129
Views
¿Cómo uso map() para asignar elementos de matriz a diferentes variables?

para aclarar que estoy usando React, Node, Postgres, Express.

Tengo una ruta expresa que devuelve una fila de tabla, donde cada elemento es una columna diferente. Quiero asignar cada uno de estos elementos a una variable, aquí hay un ejemplo de cómo se verán los resultados devueltos:

 Object { test1_id: "4", parts_id: 26, parts_material: "Plexiglass", … }​ parts_id: 26​ parts_material: "Plexiglass"​​ parts_material_length: "49.78"​​ parts_material_thickness: "1.86"​​ parts_material_width: "24.96"​​ parts_produced: 5​​ test1_id: "4"​​ workorder_id: 2​​ workorder_total: 76

Y aquí está mi intento de asignar estos elementos a variables separadas:

 let thickness1 = 0; let width1 = 0; let length1 = 0; let partNeeded = 0; // Material requirements calculation const getReq = async (id) => { try { console.log(materials); const response = await fetch(`http://localhost:5000/materials/${id}`, [id]) const jsonData = await response.json(); jsonData.map(x => x.parts_material_thickness = thickness1); console.log(jsonData) console.log('ID is: ',id) //console.log(); //console.log('Thickness is: ', thickness1); } catch (err) { console.log(err.message); } }

Mi objetivo aquí es que cada fila tenga resultados diferentes, y las variables thickness1 , etc., tendrán valores diferentes dependiendo de la matriz que se haya devuelto. En el ejemplo que proporcioné, solo intenté mapear 1 variable, pero incluso eso no funciona. Entonces, ¿cómo usaría map() para asignar los elementos de mi matriz a diferentes variables?

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

0

Creo que puede usar .map() para esta tarea, pero no en la matriz jsonData en sí, sino en la matriz de las propiedades de su objeto de fila.

 // This assumes that: // 1. The order of values in jsonData matches // with the order of properties in the row object. // 2. The row object already has properties (has columns) // but no values yet (undefined or null) const assignedRowEntries = Object.entries(rowObject).map( [key, value], index => [key, jsonData[index]] ); console.log(assignedRowEntries); // assignedRowEntries should look like this: // [ // [parts_id, value1], // [parts_material, value2], // ... // [workorder_total, value3] // ] // This is what you needed const assignedRowObject = Object.fromEntries(assignedRowEntries);

Más sobre Object.fromEntries() y Object.entries() .

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!