分享
为什么问答平台  ›  专栏  ›  技术社区  ›  Walter Monecke

将数据结构重组为新架构-javascript - Restructuring data-structure to a new Schema - Javascript

  •  2
  • Walter Monecke  · 技术社区  · 1 周前

    我需要重新构造一个已经存在的数据结构,并且不知道如何以最有效的方式来实现这一点(如果我应该做我自己的自定义功能或者使用第三方库的话)。

    数据: 可能有一个月或一天,也可能没有一个月或一天,也就是说,如果用户从未输入过数据,那么数据结构将如下所示 {} .

    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 回复  |  直到 1 周前
        1
  •  2
  •   Jack Bashford    1 周前

    使用嵌套 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; }