如何转换对象数组?

时间:2019-07-02 22:58:28

标签: javascript arrays object

我有问题。

我有那个对象数组:

const iHaveThis = [{
    question: "What's your name?",
    answer: 'dda',
    form_filled_key: 15,
  },
  {
    question: "What's your e-mail?",
    answer: 'sda@br.com',
    form_filled_key: 15,
  },
  {
    question: "What's your e-mail?",
    answer: 'dAS@be.bimc',
    form_filled_key: 14,
  },
  {
    question: "What's your name?",
    answer: 'DAS',
    form_filled_key: 14,
  },
];

我想将其转换为:

const iWillHaveThis = [{
    "What's your e-mail?": 'sda@br.com',
    "What's your name?": 'dda',
  },

  {
    "What's your e-mail?": 'dAS@be.bimc',
    "What's your name?": 'DAS',
  },
];

我该怎么做?请

我已经尝试使用reduce,map但不能正常工作。

5 个答案:

答案 0 :(得分:3)

希望获得帮助

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

编辑

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)

答案 1 :(得分:3)

您可以将一个对象键入到您的form_filled_key中。并在循环中使用键将对象添加到对象中以对它们进行分组。最后,您的解决方案将在您构建的对象的Object.values()中:

const iHaveThat = [
  {question: "What's your name?",answer: 'dda',form_filled_key: 15,},
  {question: "What's your e-mail?",answer: 'sda@br.com',form_filled_key: 15,},
  {question: "What's your e-mail?",answer: 'dAS@be.bimc',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)

答案 2 :(得分:1)

使用reduce

const iHaveThis = [{question:"What's your name?",answer:'dda',form_filled_key:15,},{question:"What's your e-mail?",answer:'sda@br.com',form_filled_key:15,},{question:"What's your e-mail?",answer:'dAS@be.bimc',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);

答案 3 :(得分:1)

您还可以创建一个ES6 generator,以先按2然后再对其进行Array.reduce批处理该数组:

const arr = [
  { question: "What's your name?", answer: 'dda', form_filled_key: 15, },
  { question: "What's your e-mail?", answer: 'sda@br.com', form_filled_key: 15, },
  { question: "What's your e-mail?", answer: 'dAS@be.bimc', 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)

答案 4 :(得分:0)

(object[key] = object[key] || {})模式可用于添加不存在的值:

const arr = [{question: "What's your name?",answer: 'dda',form_filled_key: 15,},{question: "What's your e-mail?",answer: 'sda@br.com',form_filled_key: 15,},{question: "What's your e-mail?",answer: 'dAS@be.bimc',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 )