【发布时间】:2014-11-17 16:23:23
【问题描述】:
我的 JSON 如下所示:
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
}
]
我想要做的是分组/合并每个 type 具有相同年份和月份的 date(date 字符串中的前 7 个字符)并得到这些 @987654325 的总和@的。输出应如下所示:
json = [
{
type: "big"
date: "2012-12"
qty: 10
}
{
type: "small"
date: "2012-12"
qty: 16
}
{
type: "small"
date: "2012-11"
qty: 3
}
]
我在这里找到了几个类似的问题,但没有遇到一个完全符合我要求的问题。我玩过很多从不同示例中借来的代码,但似乎无法得到我需要的结果。我现在不在家,因此我目前无法粘贴任何我尝试过的代码,但我正在寻求帮助,希望得到答案/解决方案以便稍后测试
【问题讨论】:
-
那不是 JSON,JSON 是 string 格式。这只是一个对象数组。
标签: javascript jquery arrays coffeescript