基于 JSON api 响应构建数组

下面是来自 angularjs 中 api 的 JSON 响应,


{

"counts": 

[

  [

    {

      "xcount": 0.0,

      "ycount": 0.0,

      "month": 4


    },

    {

      "xcount": 0.0,

      "ycount": 0.0,

      "month": 5


    },

    {

      "xcount": 0.0,

      "ycount": 2.85,

      "month": 6

    },

    {

      "xcount": 8.85,

      "ycount": 0.0,

      "month": 6

    },

    {

      "xcount": 10.17,

      "ycount": 0.0,

      "month": 7

    },

    {

      "xcount": 0.0,

      "ycount": 2.85,

      "month": 7

    },

    {

      "xcount": 12.0,

      "ycount": 0.0,

      "month": 8

    },

    {

      "xcount": 0.0,

      "ycount": 2.85,

      "month": 8

    },

    {

      "xcount": 0.0,

      "ycount": 2.85,

      "month": 9

    },

    {

      "xcount": 11.0,

      "ycount": 0.0,

      "month": 9

    }

  ]

]

}

根据我的要求,我希望得到如下响应


{

"counts": 

[

  [

    {

      "xcount": 0.0,

      "ycount": 0.0,

      "month": 4


    },

    {

      "xcount": 0.0,

      "ycount": 0.0,

      "month": 5


    },

    {

      "xcount": 8.85,

      "ycount": 2.85,

      "month": 6

    },


    {

      "xcount": 10.17,

      "ycount": 2.85,

      "month": 7

    },

    {

      "xcount": 12.0,

      "ycount": 2.85,

      "month": 8

    },

    {

      "xcount": 11.0,

      "ycount": 2.85,

      "month": 9

    }

  ]

]

}


不负相思意
浏览 143回答 2
2回答

湖上湖

var arr = {  "counts": [    [{        "xcount": 0.0,        "ycount": 0.0,        "month": 4      },      {        "xcount": 0.0,        "ycount": 0.0,        "month": 5      },      {        "xcount": 0.0,        "ycount": 2.85,        "month": 6      },      {        "xcount": 8.85,        "ycount": 0.0,        "month": 6      },      {        "xcount": 10.17,        "ycount": 0.0,        "month": 7      },      {        "xcount": 0.0,        "ycount": 2.85,        "month": 7      },      {        "xcount": 12.0,        "ycount": 0.0,        "month": 8      },      {        "xcount": 0.0,        "ycount": 2.85,        "month": 8      },      {        "xcount": 0.0,        "ycount": 2.85,        "month": 9      },      {        "xcount": 11.0,        "ycount": 0.0,        "month": 9      }    ]  ]}arr.counts[0] = arr.counts[0].reduce((acc, cv, i, arr) => {  if (!acc[cv.month]) {    acc[cv.month] = cv;  } else {    acc[cv.month].xcount += cv.xcount    acc[cv.month].ycount += cv.ycount  }  if (i === arr.length - 1) {    return Object.keys(acc).map(key => acc[key])  } else {    return acc;  }}, {});console.log(arr.counts[0]);使用该reduce函数,我们使用月份号作为键创建一个临时对象。这使我们能够跟踪唯一条目。当我们找到一个重复的月份时,我们将xcountand加ycount在一起。一旦我们到达列表的末尾,我们就会返回映射到它们的值的临时对象的键。导致列表仅包含唯一月份。

慕森王

正如我从您的问题中了解到的那样,要删除重复的月份,如果是这种情况,这是我上面的建议。更新:了解您关于通过 xcount、ycount 组合的观点,并添加了必要的更改。var arr = {&nbsp; &nbsp; "counts": [&nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 4,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 5,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 6,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 2.85&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 6,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 8.85,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 10.17,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 7,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 2.85&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 8,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 12,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 8,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 2.85&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 2.85&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "month": 9,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "xcount": 11,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "ycount": 0&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; ]&nbsp; };&nbsp; var tmpMonths = [];&nbsp; var tmpObjects = {};&nbsp; var results = [];&nbsp; for (var i=0;i<arr.counts[0].length;i++) {&nbsp; &nbsp; &nbsp; var obj = arr.counts[0][i];&nbsp; &nbsp; &nbsp; var key = "month_" + obj.month;&nbsp; &nbsp; &nbsp; if (obj.month && tmpMonths.indexOf(obj.month)===-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpMonths.push(obj.month);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpObjects[key] = obj;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; results.push(obj);&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpObjects[key] = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; month: obj.month,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xcount: obj.xcount + tmpObjects[key]["xcount"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ycount: obj.ycount + tmpObjects[key]["ycount"],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; }&nbsp; }&nbsp; arr.counts[0] = Object.values(tmpObjects);&nbsp; console.log(JSON.stringify(arr));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript