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/ –