将数据结构重组为新的架构-Javascript

时间:2019-07-07 11:55:54

标签: javascript arrays json object

我需要重组一个已经存在的数据结构,并且不知道如何以最有效的方式执行此操作(如果我应该执行自己的自定义功能,或者使用第三方库)。

数据: 可能有一个月或一天,也可能没有,也就是说,如果用户从未输入过数据,则数据结构将类似于{}

2019: {        // year
  7: {         // month
    6: {       // day
      activities: (4) [{…}, {…}, {…}, {…}]
      emotionsFelt: (3) ["Excited", "Nervous", "Happy"]
      isSpecialDay: false
      latitude: null
      locationId: null
      locationName: null
      locationProvider: null
      longitude: null
      mood: "Awesome"
      optionalDescription: ""
      rating: 5
      timestamp: "2019-07-06T18:52:38.022Z"
    },
    7: {
      activities: []
      emotionsFelt: (2) ["Excited", "Lively"]
      isSpecialDay: false
      latitude: 56.130366
      locationId: "ChIJ2WrMN9MDDUsRpY9Doiq3aJk"
      locationName: "Canada"
      locationProvider: "GM"
      longitude: -106.346771
      mood: "Awesome"
      optionalDescription: ""
      rating: 5
      timestamp: "2019-07-06T19:00:18.391Z"
    }
  }
}

结果应该是:

2019: {        // year
  7: {         // month
    6: {       // day
      avgRating: 5,
      isSpecialDay: false,
      entries: [
        {
          activities: [{…}, {…}, {…}, {…}],
          emotionsFelt: ["Excited", "Nervous", "Happy"],
          isSpecialMoment: false,
          latitude: null,
          locationId: null,
          locationName: null,
          locationProvider: null,
          longitude: null,
          mood: "Awesome",
          optionalDescription: "",
          rating: 5,
          images: [],
          timestamp: "2019-07-06T18:52:38.022Z",
        }
      ]
    },
    7: {
      avgRating: 5,
      isSpecialDay: false,
      entries: [
        {
          activities: [],
          emotionsFelt: ["Excited", "Lively"],
          isSpecialMoment: false,
          latitude: 56.130366
          locationId: "ChIJ2WrMN9MDDUsRpY9Doiq3aJk"
          locationName: "Canada"
          locationProvider: "GM"
          longitude: -106.346771
          mood: "Awesome",
          optionalDescription: "",
          rating: 5,
          images: [],
          timestamp: "2019-07-06T18:52:38.022Z",
        }
      ]
    }
  }
}

有关如何实现此目标的任何提示? 在此先感谢:)

1 个答案:

答案 0 :(得分:2)

使用嵌套的for...in循环,破坏,扩展和简写属性表示法,如下所示:

let obj = {2019:{7:{6:{activities:["one","two","three","four"],emotionsFelt:["Excited","Nervous","Happy"],isSpecialDay:false,latitude:null,locationId:null,locationName:null,locationProvider:null,longitude:null,mood:"Awesome",optionalDescription:"",rating:5,timestamp:"2019-07-06T18:52:38.022Z"},7:{activities:[],emotionsFelt:["Excited","Lively"],isSpecialDay:false,latitude:56.130366,locationId:"ChIJ2WrMN9MDDUsRpY9Doiq3aJk",locationName:"Canada",locationProvider:"GM",longitude:-106.346771,mood:"Awesome",optionalDescription:"",rating:5,timestamp:"2019-07-06T19:00:18.391Z"}}}};

for (let year in obj) {
  for (let month in obj[year]) {
    for (let day in obj[year][month]) {
      const { isSpecialDay, rating: avgRating, ...r } = obj[year][month][day];
      obj[year][month][day] = { isSpecialDay, avgRating, entries: [{ isSpecialDay, avgRating, ...r }]};
    }
  }
}

console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: auto; }