Closed. This question is not reproducible or was caused by typos . It is not currently accepting answers.
A few lines of modern JavaScript will get you the result you want:
var dateArr = Object.values(list.reduce((result, {
date,
type,
amount
}) => {
// Create new group
if (!result[date]) result[date] = {
date,
activities: []
};
// Append to group
result[date].activities.push({
type,
amount
});
return result;
}, {}));
Explaination:
Use Array.reduce
to consolidate the list into a set of results, a plain object, grouped by date.
The consolidate function destructure the item into three parameters.
It then creates a new group if necessary.
Current item's type and amount is then pushed to the group as part of an object literal .
The same set is returned to the reduce
, so that the next item will consolidate into the same set.
Use Object.values
to extract the values from the set. (Drop the keys)
JavaScript group an array of objects by key, In this article, I will explain about the groupBy array of object in javascript.. “ JavaScript group an array of objects by key” is published by Edison� Array.prototype.groupBy = function(prop) { } In Javascript we can declare our very own methods on objects. This means that we will be able to call our new groupBy () on any array object like <Array>.groupBy (prop). The prop will be the name of the property we want to group by.
You could use a hash table for groupiung by date and assign the single group to the result array.
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 }],
result = [];
list.forEach(function (hash) {
return function (a) {
if (!hash[a.date]) {
hash[a.date] = { date: a.date, activities: []};
result.push(hash[a.date]);
}
hash[a.date].activities.push({ type: a.type, amount: a.amount });
};
}(Object.create(null)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Group Array of JavaScript Objects by Key or Property Value � GitHub, Group Array of JavaScript Objects by Key or Property Value. Implementation. const groupBy = key => array => array.reduce((objectsByKeyValue, obj) => { const� group-objects-by-property.md Group Array of JavaScript Objects by Keys or Property Values This fork of JamieMason's implementation changes the key parameter to be an array of keys instead of just a single key. This makes it possible to group by multiple properties instead of just one.
I hope this is what you exactly need.
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));
How to group an array of objects through a key using Array reduce , Understand how to group an array of objects using one of its key. We will achieve this function using array reduce in javascript. DataGrouper itself is a function. You can call it with your data and a list of the properties you want to group by. It returns an array whose elements are object with two properties: key is the collection of grouped properties, vals is an array of objects containing the remaining properties not in the key.
consolelog.io => groupBy() in Javascript, In Javascript we can declare our very own methods on objects. This means that we will be able to call our new groupBy() on any array object like <Array>. groupBy(prop) . The prop will be the name of the property we want to group by. In this article, I will explain about the groupBy array of object in javascript.. “JavaScript group an array of objects by key” is published by Edison Devadoss.
javascript group by property array of objects Code Example, Get code examples like "javascript group by property array of objects" instantly right from your google search results with the Grepper Chrome� When we return 1, the function communicates to sort() that the object b takes precedence in sorting over the object a.Returning -1 would do the opposite.. The callback function could calculate other properties too, to handle the case where the color is the same, and order by a secondary property as well:
group objects in array by property Code Example, Get code examples like "group objects in array by property" instantly right from your google search results javascript by Bhishma'S on May 06 2020 Donate. 1. Grouping the players array: _.groupBy(players, "team"); This applies lodash’s groupBy function to the players array using the property team. Quotation marks are important. Here’s a different way to look at this function: const Array = players; const Property = "team"; const playersByTeam = _.groupBy(Array, Property);
Comments The list.forEach(
one works just fine. dateArr
just isn't an array, but an object. Try console.log(dateArr)
after your last snippet. Your first attempt returns exactly the output you described. And as Cerbrus points out, the second one works too. It seems you have a problem with how you are observing the result. mmm...curious to say the least...will have another look. console.log gives me empty arrays though. I'll have to go through it bit by bit again to see. Thanks for help though - at least I know now it should work in principal. Ah - figured it out. It was a IndexedDB transaction that was not completing in time. Dexie's then()
function fixed it. @Funktion so after a year has passed, what are your compare/test results? :P @Pascal I unfortunately don't have access to that project at the moment, but as I recall I used the last solution by chindirala - only because I tried it and it worked straight out of the box. Might need this again for a current project actually, so will see how it goes.