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

110
Views
¿Cómo distribuyo los objetos dentro de una matriz de objetos?

A continuación, agregué algunos detalles sobre los datos originales y el resultado esperado también agregué el código que probé.

Los datos originales que tengo

 original_data = [ { question_name: "What is this question for?", description: "This is a description", type: "Multi choice", answers: [ { option: "opt1", score: 2, }, { option: "opt2", score: 4, }, { option: "opt3", score: 5, }, ], }, ];

Rendimiento esperado

 expected_output = { Question: "What is this question for?", Description: "This is a description", Type: "Multi choice", option: "opt1", score: 2, option1: "opt2", score1: 4, option2: "opt3", score2: 5, };

El código que probé

 const updated_qstnData = original_data.map((d) => { const { question_name, description, type, answers, } = d; return { Question: question_name, Description: question_description, Type: answer_type, }; });

De acuerdo con el código que probé, pude crear un objeto con una pregunta , una descripción y un tipo , pero no sé cómo seleccionar la clave/valor de la matriz de respuestas de los objetos y agregarlos al objeto (updated_qstnData) que estoy creando

¿Cómo extiendo la clave/valor de respuestas a mi objeto de salida?

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

0

Compruebe si esto ayudaría en su situación:

 const original_data = [{ "question_name":"What is this question for?", "description":"This is a description", "type":"Multi choice", "answers":[ {"option":"opt1","score":2}, {"option":"opt2","score":4}, {"option":"opt3","score":5} ] }]; let new_data = original_data.map(o => { let expected = { Question: o.question_name, Description: o.description, Type: o.type, }; if (o.answers) { o.answers.forEach((a, index) => Object.keys(a).forEach(k => expected[k + (index+1)] = a[k])); } //console.log(expected); return expected; }); console.log("new_data", new_data); console.log("first item in new_data", new_data[0]);
about 4 years ago · Juan Pablo Isaza Report

0

Puedes probar esta solución

 const original_data = [ { question_name: "What is this question for?", description: "This is a description", type: "Multi choice", answers: [ { option: "opt1", score: 2, }, { option: "opt2", score: 4, }, { option: "opt3", score: 5, }, ], }, ]; const updated_qstnData = original_data.map((d) => { const { question_name, description, type, answers, } = d; const result = { Question: question_name, Description: description, Type: type, } const answerObject = answers.reduce((answerData, currentAnswer, index) => { const optionKey = index ? "option" + index : "option" const scoreKey = index ? "score" + index : "score" return { ...answerData, [optionKey]: currentAnswer.option, [scoreKey]: currentAnswer.score } }, {}) return { ...result, ...answerObject }; }); console.log(updated_qstnData)
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Puede asignar de forma plana la matriz de answers a las entradas de objetos y ajustar las claves según sea necesario.

Por ejemplo...

 const original_data = [{"question_name":"What is this question for?","description":"This is a description","type":"Multi choice","answers":[{"option":"opt1","score":2},{"option":"opt2","score":4},{"option":"opt3","score":5}]}]; const updated_qstnData = original_data.map((q) => ({ Question: q.question_name, Description: q.description, Type: q.type, ...Object.fromEntries( q.answers.flatMap((a, i) => { const suffix = i > 0 ? i : ""; return Object.entries(a).map(([k, v]) => [k + suffix, v]); }) ), })); console.log(updated_qstnData);
 .as-console-wrapper { max-height: 100% !important; }

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!