与匹配值排序和合并JSON键 [英] Sort and merge JSON keys with matching values

查看:126
本文介绍了与匹配值排序和合并JSON键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JSON是这样的:

  JSON = [
  {
    类型:大
    日期:2012年12月8日
    数量:6
  }
  {
    类型:小
    日期:2012年12月8日
    数量:9
  }
  {
    类型:大
    日期:二零一二年十二月十五日
    数量:4
  }
  {
    类型:小
    日期:2012年12月7日
    数量:7
  }
  {
    类型:小
    日期:2012-11-07
    数量:3
  }
]

我试图做的是组/合并每个键入具有日期与同一年&安培;月(第7在日期字符字符串),并得到那些数量的总和。输出应该是这样的:

  JSON = [
  {
    类型:大
    日期:2012-12
    数量:10
  }
  {
    类型:小
    日期:2012-12
    数量:16
  }
  {
    类型:小
    日期:2012-11
    数量:3
  }
]

有我在这里找到,但还没有碰到一个不正是我要找的几个类似的问题。我已经有很多code从不同的例子,借来的发挥各地,但不能完全似乎得到我需要的结果。我不回家了,所以我不能粘贴任何code,我此刻的尝试,但我寻求帮助,希望有一个答案/解决方案,测试出来以后


解决方案

这是很容易通过创建一个与您的独特组合(即类型+日期前7)命名属性的自定义对象处理。

通过您的数组环路和检查,如果你的持有人对象与你的唯一标识符命名的现有属性。如果有物业已,然后增加量,否则增加一个新项目。

持有人完全建成后,清除您的数组,然后遍历持有人的性质和他们推回阵列。

\r
\r

VAR持有人= {};\r
VAR JSON = [\r
  {\r
    类型:大,\r
    日期:2012年12月8日\r
    数量:6\r
  },\r
  {\r
    类型:小,\r
    日期:2012年12月8日\r
    数量:9\r
  },\r
  {\r
    类型:大,\r
    日期:二零一二年十二月十五日\r
    数量:4\r
  },\r
  {\r
    类型:小,\r
    日期:2012年12月7日\r
    数量:7\r
  },\r
  {\r
    类型:小,\r
    日期:2012-11-07,\r
    数量:3\r
  }\r
];\r
\r
json.forEach(函数(元素){\r
  变种标识符= element.type + element.date.slice(0,7);\r
  如果(持有人[符]){\r
    持有人[符] .qty + = element.qty;\r
  }其他{\r
    持有人[名称] =元素;\r
  };\r
});\r
\r
JSON = [];\r
为(以持有人VAR标识符){\r
  json.push(架[标识]);\r
}\r
\r
的console.log(JSON);

\r

\r
\r

My JSON looks like this:

json = [
  {
    type: "big"
    date: "2012-12-08"
    qty: 6
  }
  {
    type: "small"
    date: "2012-12-08"
    qty: 9
  }
  {
    type: "big"
    date: "2012-12-15"
    qty: 4
  }
  {
    type: "small"
    date: "2012-12-07"
    qty: 7
  }
  {
    type: "small"
    date: "2012-11-07"
    qty: 3
  }
]    

What I'm trying to do is group/merge each type that has a date with the same year & month (the first 7 characters in the date string) and get the sum of those qty's. The output should look like this:

json = [
  {
    type: "big"
    date: "2012-12"
    qty: 10
  }
  {
    type: "small"
    date: "2012-12"
    qty: 16
  }
  {
    type: "small"
    date: "2012-11"
    qty: 3
  }
]  

There are several similar questions I've found here but haven't come across one that does exactly what I'm looking for. I have played around with lots of code borrowed from different examples but can't quite seem to get the results I need. I'm not home now, therefore I can't paste any code I've tried at the moment but I'm asking for help in hopes to have an answer/solution to test out later

解决方案

This is easily handled by creating a custom object that has properties named with your unique combinations (i.e. type + first 7 of date).

Loop through your array and check if your "holder" object has an existing property named with your unique identifier. If it has the property already, then increment the quantity, otherwise add a new item.

After the holder is completely built, clear your array, then loop through the properties of the holder and push them back on to your array.

var holder = {};
var json = [
  {
    type: "big",
    date: "2012-12-08",
    qty: 6
  },
  {
    type: "small",
    date: "2012-12-08",
    qty: 9
  },
  {
    type: "big",
    date: "2012-12-15",
    qty: 4
  },
  {
    type: "small",
    date: "2012-12-07",
    qty: 7
  },
  {
    type: "small",
    date: "2012-11-07",
    qty: 3
  }
];

json.forEach(function(element) {
  var identifier = element.type + element.date.slice(0, 7);
  if (holder[identifier]) {
    holder[identifier].qty += element.qty;
  } else {
    holder[identifier] = element;
  };
});

json = [];
for(var identifier in holder) {
  json.push(holder[identifier]);
}

console.log(json);

这篇关于与匹配值排序和合并JSON键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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