javascript for group object properties based on another property
var list = [
{ date: "2017-01-01",
type: "type1",
amount: 100
},
{ date: "2017-01-01",
type: "type2",
amount: 150
},
{ date: "2017-01-02",
type: "type1",
amount: 200
}]
var dateArrKeyHolder = [];
var dateArr = [];
list.forEach(function(item){
dateArrKeyHolder[item.date] = dateArrKeyHolder[item.date]||{};
var obj = dateArrKeyHolder[item.date];
if(Object.keys(obj).length == 0)
dateArr.push(obj);
obj.date = item.date;
obj.activities = obj.activities || [];
obj.activities.push({type:item.type, amount: item.amount });
});
console.log(JSON.stringify(dateArr));
/* Result:
[{"date":"2017-01-01","activities":[{"type":"type1","amount":100},{"type":"type2","amount":150}]},
{"date":"2017-01-02","activities":[{"type":"type1","amount":200}]}]
*/
Also in JavaScript:
- angular material upload file
- check browser locale javascript
- material ui icon button
- nodejs json data serving
- javascript log to console
- how to check the extension of a file in javascript
- how to hide header in react navigation
- javascript date difference in months
- jquery check if screen size
- how to write img jsx
- javascript does not equal
- get day js
- creating a nested loop of a chessboard in javascript
- js check if string is number
- JsonConvert.DeserializeObject convert into dynamic datatable
- if else jquery click function
- js what does var mean
- add coustom handlechange fprmik
- trigger button click jquery
- javascript minute and second to convert seconds
- javascript reference file two folders up
- sonarqube for angular application
- javascript detect textarea change
- string contains js