Jota
Jota

Reputation: 865

How transform that array of objects?

i have a problem.

I have that array of objects:

const iHaveThis = [{
    question: "What's your name?",
    answer: 'dda',
    form_filled_key: 15,
  },
  {
    question: "What's your e-mail?",
    answer: '[email protected]',
    form_filled_key: 15,
  },
  {
    question: "What's your e-mail?",
    answer: '[email protected]',
    form_filled_key: 14,
  },
  {
    question: "What's your name?",
    answer: 'DAS',
    form_filled_key: 14,
  },
];

I want transform it to:

const iWillHaveThis = [{
    "What's your e-mail?": '[email protected]',
    "What's your name?": 'dda',
  },

  {
    "What's your e-mail?": '[email protected]',
    "What's your name?": 'DAS',
  },
];

How can i make that ? Please

I already tried use reduce, map but not working.

Upvotes: 2

Views: 85

Answers (5)

Slai
Slai

Reputation: 22906

The (object[key] = object[key] || {}) pattern can be used to add values if they don't exist :

const arr = [{question: "What's your name?",answer: 'dda',form_filled_key: 15,},{question: "What's your e-mail?",answer: '[email protected]',form_filled_key: 15,},{question: "What's your e-mail?",answer: '[email protected]',form_filled_key: 14,},{question: "What's your name?",answer: 'DAS',form_filled_key: 14}]

const result = Object.values(arr.reduce((o, v, i) => 
    ((o[i = v.form_filled_key] = o[i] || {})[v.question] = v.answer, o), {}))

console.log( result )

Upvotes: 0

Mark
Mark

Reputation: 92481

You can make an object keyed to your form_filled_key. And in a loop add objects to the object using the key to group them. In the end, your solution will be in the Object.values() of the object you built:

const iHaveThat = [
  {question: "What's your name?",answer: 'dda',form_filled_key: 15,},
  {question: "What's your e-mail?",answer: '[email protected]',form_filled_key: 15,},
  {question: "What's your e-mail?",answer: '[email protected]',form_filled_key: 14,},
  {question: "What's your name?",answer: 'DAS',form_filled_key: 14,},];

let arr = iHaveThat.reduce((obj, {form_filled_key, question, answer}) => {

    // make a new entry if needed
    if (!obj[form_filled_key]) obj[form_filled_key] = {}

    // add the key value pair
    obj[form_filled_key][question] = answer

    return obj
},{})

// you just want the array from `values()`
let result = Object.values(arr)
console.log(result)

Upvotes: 3

Akrion
Akrion

Reputation: 18525

You could also create an ES6 generator to batch the array by 2 and then Array.reduce over it:

const arr = [
  { question: "What's your name?", answer: 'dda', form_filled_key: 15, },
  { question: "What's your e-mail?", answer: '[email protected]', form_filled_key: 15, },
  { question: "What's your e-mail?", answer: '[email protected]', form_filled_key: 14, },
  { question: "What's your name?", answer: 'DAS', form_filled_key: 14, },
];

function* batch (arr, n=2) {
  let i = 0
  while (i < arr.length) {
    yield arr.slice(i, i + n).reduce((r,c) => (r[c.question] = c.answer, r), {})
    i += n
  }
}

let result = [...batch(arr)]

console.log(result)

Upvotes: 1

Adil Liaqat
Adil Liaqat

Reputation: 229

Hope this help

iHaveThat.map((v) => ({
   [v.question]:v.answer
}))

EDIT

var obj = {};
iHaveThat.forEach((v) => {
 // Checking if the key is available in the object or not. 
 //If key isn't available it will create the object for the key.
 if(!obj[v.form_filled_key])
  obj[v.form_filled_key] = { }
 // If object is already created we will just add new field in the object
 obj[v.form_filled_key][v.question] = v.answer
})
// To convert object into array of objects.
obj = Object.values(obj)

Upvotes: 3

Jack Bashford
Jack Bashford

Reputation: 44145

Use reduce.

const iHaveThis = [{question:"What's your name?",answer:'dda',form_filled_key:15,},{question:"What's your e-mail?",answer:'[email protected]',form_filled_key:15,},{question:"What's your e-mail?",answer:'[email protected]',form_filled_key:14,},{question:"What's your name?",answer:'DAS',form_filled_key:14,}];

const res = Object.values(iHaveThis.reduce((a, { question, answer, form_filled_key }) => {
  (a[form_filled_key] = a[form_filled_key] || {})[question] = answer;
  return a;
}, {}));

console.log(res);

Upvotes: 1

Related Questions