5

I have two variable like this, one is array of objects and one is object of objects

let state_checklist = [
  {
    id: '1',
    title: 'Q1',
    question_id: 'CuaQV',
  },
  {
    id: '2',
    title: 'Q3',
    question_id: 'XKVbQ',
  },
  {
    id: '3',
    title: 'Q2',
    question_id: 'zmId1',
  },
];

let state_question = {
  2: { answer: 'yes', comments: '', question_id: 'CuaQV' },
  3: { answer: 'no', comments: '', question_id: 'zmId1' },
};

Now I want to create a structure like this

{
    "zmId1": {
        "answer": "yes",
        "comments": "",
        "question_id": "zmId1",
        "title": "Q2"
    },
    "CuaQV": {
        "answer": "no",
        "comments": "",
        "question_id": "CuaQV",
        "title": "Q1"
    }
}

where key should be question_id

Code that I have tried to generate that object is below, here I am unable to create the question_id as key, else everything is seems ok for me.

//var obj = {};
for (var key in state_question) {
  if (state_question.hasOwnProperty(key)) {
    //var key = state_question[key]['question_id'];
    const questionid = state_question[key]['question_id'];
    const title = state_checklist.find(
      (q) => q.question_id == questionid
    ).title;
    state_question[key]['title'] = title;
    //obj[key] = state_question[key];
    console.log(title);
  }
}

console.log(state_question);
3
  • I think you should give more detail on the Data Structure and intent of your code. Commented Oct 19, 2021 at 12:27
  • 1
    is your sample correct? state_question zmId1 - answer - no, expected result, answer is yes. Commented Oct 19, 2021 at 12:29
  • Ya and the answer that was given is seems ok to me. I was trying to implement something similar Commented Oct 19, 2021 at 12:33

2 Answers 2

2

You can loop through your checklist and then map the values to an object and look up the other object array values with what exist there and missed in your result

const questions = {};
for (const q of state_checklist) {
   const answerFound = Object.values(state_question).find(x => q.question_id === x.question_id);
   if (answerFound) {
      questions[q.question_id] = {
         question_id: q.question_id,
         title: q.title,
         answer: answerFound.answer,
         comments: answerFound.comments
      }
   }
}

// Result
// { CuaQV: { question_id: 'CuaQV', title: 'Q1', answer: 'yes', comments: '' },
//   zmId1: { question_id: 'zmId1', title: 'Q2', answer: 'no', comments: '' } 
// }

Sign up to request clarification or add additional context in comments.

Comments

0

Basically a shorter answer to use reduce function of Array produced by Object.values(state_question), and compose the object (starting with {}) as below:

const result = Object.values(state_question).reduce((acc, val) => ({
        ...acc,
        [val.question_id]: {
          // ...state_checklist.find(item => item.question_id === val.question_id),
          title: state_checklist.find(item => item.question_id === val.question_id).title,
          ...val,
        }
    }), {});

console.log(result);

// { CuaQV: { question_id: 'CuaQV', title: 'Q1', answer: 'yes', comments: '' },
//   zmId1: { question_id: 'zmId1', title: 'Q2', answer: 'no', comments: '' } 
// }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.