0

I'm trying to map the answer property based on the type to a new single object

const questionsArray = [
  {
    question: `What is your name?`,
    type: "name"
  },
  {
    question: `What is your age?`,
    type: "age"
  }
]

Which will result to this:

bday = {
          name: 'Jose',
          age: '23',
        }

This won't work because every question will replace the previous value already set since the type will be different

2
  • can the types match with the key in the resulting object? e.g does birthDay have to be that or can it use the type of day and thus be the key day instead? Commented Feb 23, 2020 at 5:51
  • 1
    @NickParsons yes you are right! it can be like you say Commented Feb 23, 2020 at 5:54

2 Answers 2

2

You can use reduce() and use type as key and answer as value for that key

const questionsArray = [  {    id: 0,    question: `What is your name?`,    answer: 'jose',    type: "name"  },  {    id: 1,    question: `What is your birth month?`,    answer: 'January',    type: "month"  },  {    id: 2,    question: `What day in were you born?`,    answer: '24',    type: "day"  },  {    id: 3,    question: `What's your email?`,    answer: '[email protected]',    type: 'email'  }]

const res = questionsArray.reduce((ac, {type, answer}) => {
  ac[type] = answer;
  return ac;
}, {});
console.log(res)

As you will notice in the above way you can't use any other keys other than type property of array values.

If you want to have custom property names in resulting object you need to have an hash table.

const questionsArray = [  {    id: 0,    question: `What is your name?`,    answer: 'jose',    type: "name"  },  {    id: 1,    question: `What is your birth month?`,    answer: 'January',    type: "month"  },  {    id: 2,    question: `What day in were you born?`,    answer: '24',    type: "day"  },  {    id: 3,    question: `What's your email?`,    answer: '[email protected]',    type: 'email'  }]

const table = {
  day: 'birthDay',
  month: 'birthMonth'
}

const res = questionsArray.reduce((ac, {type, answer}) => {
  ac[table[type] || type] = answer;
  return ac;
}, {});
console.log(res)

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

Comments

2

You can use .map() to map each object to an new object with which has the type as the key and the answer as the value. You can then use Object.assing() to build one larger resulting object from this array of mapped objects:

const questionsArray = [ { id: 0, question: `What is your name?`, answer: 'jose', type: "name" }, { id: 1, question: `What is your birth month?`, answer: 'January', type: "month" }, { id: 2, question: `What day in were you born?`, answer: '24', type: "day" }, { id: 3, question: `What's your email?`, answer: '[email protected]', type: 'email' } ];

const bday = Object.assign(...questionsArray.map(({answer, type}) => ({[type]: answer})));
console.log(bday);

1 Comment

Nice combination of all the features

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.