如何在JavaScript中将对象数组转换为数组对象?

时间:2019-12-13 12:27:09

标签: javascript arrays object

我正在尝试使用javascript将对象数组转换为数组对象。

这是代码

const data = [{
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
             {
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
            {
  yearMonthKey: "201907",
  dayKey: "16",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma12313"
},
            {
  yearMonthKey: "201908",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
}]

let programs = {};

我想使用程序对象来像下面这样制作一个对象。

{
  201907: {
    15: [{
      yearMonthKey: "201907",
      dayKey: "15",
      startDate: "2019-07-15 00:00:00+0900",
      title: "testProgrma"
    },
        {
      yearMonthKey: "201907",
      dayKey: "15",
      startDate: "2019-07-15 00:00:00+0900",
      title: "testProgrma123132"
    }],
    16: [{
        yearMonthKey: "201907",
        dayKey: "16",
        startDate: "2019-07-15 00:00:00+0900",
        title: "testProgrma"
      }]
  },
    201908: {
      15: [{
        yearMonthKey: "201908",
        dayKey: "15",
        startDate: "2019-07-15 00:00:00+0900",
        title: "testProgrma"
      }]
    }
}

我尝试使用映射数组方法解决它。

data.map(item => {
  programs[item.yearMonthKey] : {
    programs[item.dayKey] : [{

    }]
  }
})

,但是将对象作为数组中相同的 dayKey 键的值进行排序并将其放入相同的 yearMonthKey 内是有点挑战的。

5 个答案:

答案 0 :(得分:5)

您可以reduce阵列。将每个yearMonthKey添加到累加器。在yearMonthKey的基础上,向dayKey对象添加一个嵌套的acc[yearMonthKey]键,并将其默认为数组。

const data=[{yearMonthKey:"201907",dayKey:"15",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma"},{yearMonthKey:"201907",dayKey:"15",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma"},{yearMonthKey:"201907",dayKey:"16",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma12313"},{yearMonthKey:"201908",dayKey:"15",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma"}];

const output = data.reduce((acc, o) => {
  if (!acc[o.yearMonthKey]) 
    acc[o.yearMonthKey] = {};
    
  if (!acc[o.yearMonthKey][o.dayKey]) 
    acc[o.yearMonthKey][o.dayKey] = [];
    
  acc[o.yearMonthKey][o.dayKey].push(o);
  
  return acc;
}, {})

console.log(output)

答案 1 :(得分:0)

const data = [{
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
             {
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
            {
  yearMonthKey: "201907",
  dayKey: "16",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma12313"
},
            {
  yearMonthKey: "201908",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
}]

let programs = {};

data.forEach(function(element) {

    if (typeof programs[element.yearMonthKey] == "undefined") {
        programs[element.yearMonthKey] = {};
    }
    
    if(typeof programs[element.yearMonthKey][element.dayKey] == "undefined") {
        programs[element.yearMonthKey][element.dayKey] = [];
    }
    
    programs[element.yearMonthKey][element.dayKey].push(
        element
    );
});

console.log(programs);

答案 2 :(得分:0)

const data=[{yearMonthKey:"201907",dayKey:"15",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma"},{yearMonthKey:"201907",dayKey:"15",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma"},{yearMonthKey:"201907",dayKey:"16",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma12313"},{yearMonthKey:"201908",dayKey:"15",startDate:"2019-07-15 00:00:00+0900",title:"testProgrma"}];

const dist_yearMonthKeys = [...new Set(data.map(d => d.yearMonthKey))];

let program = {};
for (let ymk of dist_yearMonthKeys) {
    const ymk_num = parseInt(ymk); // error-prone, be careful with your source data
    program[ymk_num] = {};
    const dist_dayKeys = [...new Set(data.filter(d => d.yearMonthKey === ymk).map(d => d.dayKey))];
    for (let dk of dist_dayKeys) {
        const dk_num = parseInt(dk); // error-prone
        program[ymk_num][dk_num] = data.filter(d => d.dayKey === dk && d.yearMonthKey === ymk);
    }
}

console.log(program);

这将提供数字键,如您所要求的。

答案 3 :(得分:0)

使用任何按键进行分组的通用功能

function group() {
   const args = Array.from(arguments)
   const data = args.shift()

   const result = {}
   data.forEach(elem => {
      let level = result
      args.forEach((groupProp, ind) => {
         const groupValue = elem[groupProp]
         if (groupValue in level) {
            level = level[groupValue]
         } else {
            level = level[groupValue] = ind === args.length - 1 ? [] : {}
         }
      })
      level.push(elem)
   })

   return result   
}

const data = [{
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
             {
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
            {
  yearMonthKey: "201907",
  dayKey: "16",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma12313"
},
            {
  yearMonthKey: "201908",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
}]

const result = group(data, 'yearMonthKey', 'dayKey')
console.log(result)

答案 4 :(得分:0)

尝试破坏列表,然后以所需的格式将其放回去

const data = [{
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
             {
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
},
            {
  yearMonthKey: "201907",
  dayKey: "16",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma12313"
},
            {
  yearMonthKey: "201908",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
}]

const [1,2,3,4] = data

现在,当您调用变量1时,您将得到

{
  yearMonthKey: "201907",
  dayKey: "15",
  startDate: "2019-07-15 00:00:00+0900",
  title: "testProgrma"
} 

现在,您可以单独处理每个对象,而不必在列表中。这样,您基本上可以对对象进行任何操作