2

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.

4
  • 1
    @MarkMeyer yes, i fixed it, sorry Commented Jul 2, 2019 at 23:01
  • 1
    Is form_filled_key the determining field on wither or not the objects belong together? If so you should mention that criteria in the question otherwise you need to explain what is the determining factor Commented Jul 2, 2019 at 23:02
  • So, there may be one or more objects Commented Jul 2, 2019 at 23:03
  • 1
    @PatrickEvans Yesss,it is form_filled_key Commented Jul 2, 2019 at 23:04

5 Answers 5

3

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)

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

Comments

3

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)

3 Comments

That wouldnt combine the objects, that would just produce a 1to1 transform which op isnt after
Hello Adill, thanks for your answer.. i already tried it but as Patrick told, it doesnt combine the objects
You should add a description/explanation of what your code is doing. Code dumps dont teach.
1

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

Comments

1

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)

Comments

0

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 )

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.