如何在javascript中将对象数组转换为数组对象? [英] How to convert array of objects to object of array in javascript?

查看:59
本文介绍了如何在javascript中将对象数组转换为数组对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是代码

const 数据 = [{yearMonthKey: "201907",dayKey: "15",开始日期: "2019-07-15 00:00:00+0900",标题:测试程序"},{yearMonthKey: "201907",dayKey: "15",开始日期: "2019-07-15 00:00:00+0900",标题:测试程序"},{yearMonthKey: "201907",dayKey: "16",开始日期: "2019-07-15 00:00:00+0900",标题:testProgrma12313"},{yearMonthKey: "201908",dayKey: "15",开始日期: "2019-07-15 00:00:00+0900",标题:测试程序"}]让程序 = {};

我想使用程序对象来制作一个对象,如下所示.

<预><代码>{201907:{15: [{yearMonthKey: "201907",dayKey: "15",startDate: "2019-07-15 00:00:00+0900",标题:测试程序"},{yearMonthKey: "201907",dayKey: "15",startDate: "2019-07-15 00:00:00+0900",标题:testProgrma123132"}],16: [{yearMonthKey: "201907",dayKey: "16",开始日期: "2019-07-15 00:00:00+0900",标题:测试程序"}]},201908:{15: [{yearMonthKey: "201908",dayKey: "15",开始日期: "2019-07-15 00:00:00+0900",标题:测试程序"}]}}

我尝试使用数组方法中的映射来解决它.

data.map(item => {程序[item.yearMonthKey]:{程序[item.dayKey]:[{}]}})

但是将对象作为数组中相同 dayKey 键的值进行排序并将它们放在相同的 yearMonthKey 中是有点挑战性的.

解决方案

你可以 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 输出 = data.reduce((acc, o) => {如果 (!acc[o.yearMonthKey])acc[o.yearMonthKey] = {};if (!acc[o.yearMonthKey][o.dayKey])acc[o.yearMonthKey][o.dayKey] = [];acc[o.yearMonthKey][o.dayKey].push(o);返回acc;}, {})console.log(输出)

I am trying to convert array of objects to object of array using javascript.

Here is the code

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 = {};

I want to use programs object to make an object out of it like this below.

{
  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"
      }]
    }
}

I try to solve it using map in array method.

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

    }]
  }
})

but it is bit challenging to sort objects as a value of same dayKey key inside of an array and put those inside of same yearMonthKey.

解决方案

You could reduce the array. Add each yearMonthKey to the accumulator. Based on the yearMonthKey add a nested dayKey key to the acc[yearMonthKey] object and default it to an array.

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)

这篇关于如何在javascript中将对象数组转换为数组对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆