0
我有出外下面合并多个JSON对象iniside数组到期望的格式
[{
"data": {
"aggregations": {
"openissue": {
"buckets": [{
"doc_count": 5,
"date": "25 aug"
},
{
"doc_count": 6,
"date": "26 aug"
},
{
"doc_count": 7,
"date": "27 aug"
},
{
"doc_count": 8,
"date": "28 aug"
}
]
}
}
},
"a": "not needed",
"b": "not needed"
},
{
"data": {
"aggregations": {
"closeissue": {
"buckets": [{
"doc_count": 9,
"date": "29 aug"
},
{
"doc_count": 10,
"date": "30 aug"
},
{
"doc_count": 11,
"date": "31 aug"
},
{
"doc_count": 12,
"date": "1 sept"
}
]
}
}
},
"a": "not needed",
"b": "not needed"
}, {
"data": {
"aggregations": {
"pendingissue": {
"buckets": [{
"doc_count": 13,
"date": "10 sept"
},
{
"doc_count": 14,
"date": "13 sept"
},
{
"doc_count": 15,
"date": "18 sept"
},
{
"doc_count": 16,
"date": "19 sept"
}
]
}
}
},
"a": "not needed",
"b": "not needed"
}
]
对象的数目是该我被AJAX调用获得动态的,这是一个JSON文件json是在ajax调用之后创建的。我试图过滤掉json,并将其转换为所需的格式,其中对象的数量取决于日期的数量。
所需的输出应该像
[
{
"Key_as_string": "25aug",
"values": [
{
"label": "open-issues",
"doc_count": 10
},
{
"label": "closed-issues",
"doc_count": 10
},
{
"label": "exempted-issues",
"doc_count": 10
}
]
},
{
"Key_as_string": "26aug",
"values": [
{
"label": "open-issues",
"doc_count": 10
},
{
"label": "closed-issues",
"doc_count": 10
},
{
"label": "pending-issues",
"doc_count": 10
}
]
}
]
使用JavaScript或jQuery的
非常感谢您的回复。我不熟悉loadsh。所以我会尝试使用普通的javascript。在这种情况下,代码不会改变很多吗?对不起,我在JavaScript中很新。 –
@TrinanjanSaha _.groupBy是唯一使用lodash的地方。所以,如果你想使用原生的JavaScript,那么在纯JavaScript中有如何做到这一点。 https://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects。 – spiritwalker
我正在尝试,因为你提到。我得到一些error.this是小提琴附加http://jsfiddle.net/y45Lfupw/62/ –