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

267
Views
¿Empujar elementos en una matriz en Javascript?

Estoy trabajando en school-app . persona ingrese las marks de los estudiantes desde la interfaz y tengo que almacenarlas en mi backend. Sé que mi estructura de datos es bastante mala. pero esta es la única forma en que puedo usarlo cómodamente y colocarlo en mi aplicación/sitio web.

enlace de codeSandbox

Código completo:

 //This data is already set need to push information in this array. let student = [{ "detail": { "name": "Mark", "surname":"widen" }, }]; //formatting the query in json. const keys = Object.keys(query)[0].split(",") const values = Object.values(query)[0].split(",") const newObj = {} for (let i = 0; i < keys.length; i++) { newObj[keys[i]] = values[i] } // I've to push it along with "academic-year". so, for (let a = 0; a < newObj.length; a++) { const year = a + "st-Year" console.log(year) // Expected Output: 1st-Year and 2nd-Year } // How to run this both for-loop synchronously way ?? AND //pushing with "ObtainedMarks" and "year" (Error over here) student.push({ ObtainedMarks: { year : [ { physics: newObj } ], year : [ { physics: newObj } ] } }) console.log(student) //Here's I want expected Output

Rendimiento esperado:

 let student = [{ "detail": { "name": "Mark", "surname":"widen" }, ObtainedMarks: { "1st-Year": [ { physics: { "marks": "500" } } //Physics subject is default. ], "2nd-Year": [ { physics: { "mark": "200" } } //Physics subject is default. ] } }];

Quiero enviar datos devueltos en la matriz de estudiantes. con bucle for de 1st-Year y 2nd-Year's .

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

0

Puedes hacer la conversión en tu for-loop

 let student = [{ "detail": { "name": "Mark", "surname": "widen" }, }]; let query = { "marks,mark": "500,200" } const keys = Object.keys(query)[0].split(","); const values = Object.values(query)[0].split(","); const marks = {} for (let i = 0; i < keys.length; i++) { marks[i === 0 ? `${i+1}st-year` : `${i+1}nd-year`] = [{ physics: { [keys[i]]: values[i] } }]; } student.push({ obtainedMarks: marks }); console.log(student);

Forma alternativa: map a través de las keys y crear un objeto from entries después de manipular los datos.

 let student = [{ "detail": { "name": "Mark", "surname": "widen" }, }]; let query = { "marks,mark": "500,200" } const keys = Object.keys(query)[0].split(","); const values = Object.values(query)[0].split(","); const marks = Object.fromEntries(keys.map((k, i) => { return [ i === 0 ? `${i+1}st-year`: `${i+1}nd-year`, [{ physics: { [k]: values[i] }}] ]; })); student.push({ obtainedMarks: marks }); console.log(student);

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!