[英]Get multiple sets from JSON for different keys in Javascript
嗨,我被這個問題困住了。 希望有人能給我一些建議。
我有一個 JSON,其中包含我正在處理的應用程序的最后一天更改。 我想從中獲取 3 個數據集。 並希望保持正確的日期順序,如下例所示:
"log": [{
"objectname": "Lorem Ipsum",
"type": "CREATE",
"timenormal": "Sun Feb 09 2020 22:02:26 GMT+0100",
"closesthours": 22
}, {
"objectname": "This is a Dummy",
"type": "CREATE",
"timenormal": "Sun Feb 09 2020 23:27:46 GMT+0100",
"closesthours": 23
},{
"objectname": "Deleted test",
"type": "DELETE",
"timenormal": "Mon Feb 10 2020 00:30:14 GMT+0100",
"closesthours": 1
},{
"objectname": "Qwerty",
"type": "CREATE",
"timenormal": "Mon Feb 10 2020 00:45:04 GMT+0100",
"closesthours": 1
},{
"objectname": "Deleted test",
"type": "DELETE",
"timenormal": "Mon Feb 10 2020 04:45:14 GMT+0100",
"closesthours": 5
}, {
"objectname": "Hello World",
"type": "CREATE",
"timenormal": "Mon Feb 10 2020 10:51:22 GMT+0100",
"closesthours": 11
}, {
"objectname": "Another one",
"type": "CREATE",
"timenormal": "Mon Feb 10 2020 10:41:22 GMT+0100",
"closesthours": 11
}, {
"objectname": "ABC",
"type": "CREATE",
"timenormal": "Mon Feb 10 2020 21:16:57 GMT+0100",
"closesthours": 21
}, {
"objectname": "test dummy",
"type": "DELETE",
"timenormal": "Mon Feb 10 2020 21:17:00 GMT+0100",
"closesthours": 21
}
]
我知道以下可以使用地圖作為“最近的時間”:
jsondata['log'].map(function(value, index) {return value['closesthours']});
[22, 23, 1, 1, 5, 11, 11, 21, 21]
從那以后,我被困住了。 我想要的3 個數據集是:
1.)結合最近時間的地圖:
{"hours":"22,23,1,5,11,21"}
2 & 3)然后我真的想通過最接近的小時數在數據集中為兩種類型 (DELETE 和 CREATE) 計算出現次數例如,我想獲得以下結果(如果該類型沒有 te,則為 0最近時間):
{"type":"CREATE","groupcount":"1,1,1,0,2,1"}
{"type":"DELETE","groupcount":"0,0,1,1,0,1"}
我怎樣才能在 javascript 中做到這一點?
您可以首先使用唯一的小時數組和每種類型構建一個對象,然后使用另一個循環使用先前創建的對象為每種類型填充groupcount
數組。
const data = [{"objectname":"Lorem Ipsum","type":"CREATE","timenormal":"Sun Feb 09 2020 22:02:26 GMT+0100","closesthours":22},{"objectname":"This is a Dummy","type":"CREATE","timenormal":"Sun Feb 09 2020 23:27:46 GMT+0100","closesthours":23},{"objectname":"Deleted test","type":"DELETE","timenormal":"Mon Feb 10 2020 00:30:14 GMT+0100","closesthours":1},{"objectname":"Qwerty","type":"CREATE","timenormal":"Mon Feb 10 2020 00:45:04 GMT+0100","closesthours":1},{"objectname":"Deleted test","type":"DELETE","timenormal":"Mon Feb 10 2020 04:45:14 GMT+0100","closesthours":5},{"objectname":"Hello World","type":"CREATE","timenormal":"Mon Feb 10 2020 10:51:22 GMT+0100","closesthours":11},{"objectname":"Another one","type":"CREATE","timenormal":"Mon Feb 10 2020 10:41:22 GMT+0100","closesthours":11},{"objectname":"ABC","type":"CREATE","timenormal":"Mon Feb 10 2020 21:16:57 GMT+0100","closesthours":21},{"objectname":"test dummy","type":"DELETE","timenormal":"Mon Feb 10 2020 21:17:00 GMT+0100","closesthours":21}] const {hours, types} = data.reduce((r, {type, closesthours}, i) => { r.hours = [...new Set([...r.hours, closesthours] || [])] r.types[type] = {type, groupcount: []} return r; }, {hours: [], types: {}}); data.forEach(({type, closesthours}) => { const index = hours.indexOf(closesthours); for(let t in types) { if(types[t].groupcount[index] == undefined) { types[t].groupcount[index] = 0 } if(t == type) { types[t].groupcount[index] += 1 } } }); console.log(hours) console.log(Object.values(types))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.