1

Below I added some details about the original data and the expected output also added the code I tried.

The original data I have

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,
          },
        ],
      },
    ];

expected Output

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,
};

The Code I tried

const updated_qstnData = original_data.map((d) => {
  const {
    question_name,
    description,
    type,
    answers,
  } = d;

  return {
    Question: question_name,
    Description: question_description,
    Type: answer_type,
  };
});

According to the code which I tried, I was able to create an object with a Question, Description, and Type but don't know how to pick the key/value from the answers array of objects and add them to the object (updated_qstnData) that I am creating

How do I spread out the answers key/value to my output object?

2
  • 2
    regarding expected_output , you can't have 2 same keys at once in an object Commented Apr 20, 2022 at 3:49
  • Thank you for the valuable inputs I edited my excepted_output now all key names are different Commented Apr 20, 2022 at 3:55

4 Answers 4

2

Check if this would help in your situation:

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]);
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this solution

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; }

Comments

0

You can flat map the answers array to object entries and adjust the keys as necessary.

For example...

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; }

Comments

-1

You can't have two keys with the same name in an object so perhaps you can use a number suffix as follows:

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, } ] } ],

updated_qstnData = original_data.map((d) => {
  const {
    question_name,
    description,
    type,
    answers
  } = d;

  return {
    Question: question_name,
    Description: description,
    Type: type,
    ...answers.reduce(
        (acc,{option,score},i) => 
        ({...acc,[`option_${i+1}`]:option,[`score_${i+1}`]:score}), {}
    )
  };
});

console.log( updated_qstnData[0] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.