javascript | Object grouping
Try with something like this:
function groupBy(collection, property) {
var i = 0, val, index,
values = [], result = [];
for (; i < collection.length; i++) {
val = collection[i][property];
index = values.indexOf(val);
if (index > -1)
result[index].push(collection[i]);
else {
values.push(val);
result.push([collection[i]]);
}
}
return result;
}
var obj = groupBy(list, "group");
Keep in mind that Array.prototype.indexOf
isn't defined in IE8 and older, but there are common polyfills for that.
How to group array of objects by certain property values
A back to the future answer :
Not yet supported by lot of browsers but will come soon (Stage 3 for TC39) and already available in polyfill core-js) is the new groupBy
method on the array object.
This will allows you to do it directly like this :
employees.groupBy(employee => employee.company);
or even :
employees.groupBy(({company}) => company);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/groupBy
How can I group an array of objects by key?
Timo's answer is how I would do it. Simple _.groupBy
, and allow some duplications in the objects in the grouped structure.
However the OP also asked for the duplicate make
keys to be removed. If you wanted to go all the way:
var grouped = _.mapValues(_.groupBy(cars, 'make'),
clist => clist.map(car => _.omit(car, 'make')));
console.log(grouped);
Yields:
{ audi:
[ { model: 'r8', year: '2012' },
{ model: 'rs5', year: '2013' } ],
ford:
[ { model: 'mustang', year: '2012' },
{ model: 'fusion', year: '2015' } ],
kia:
[ { model: 'optima', year: '2012' } ]
}
If you wanted to do this using Underscore.js, note that its version of _.mapValues
is called _.mapObject
.
Most efficient method to groupby on an array of objects
If you want to avoid external libraries, you can concisely implement a vanilla version of groupBy()
like so:
var groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
console.log(groupBy(['one', 'two', 'three'], 'length'));
// => {"3": ["one", "two"], "5": ["three"]}
How to group items in an array by property using reduce and return an array of new objects
You should be able to do this in a few lines using reduce, we create a map using the Subdomain name as the key, then we'll use Object.values to turn the resulting object into an array.
For example:
const relatedSites = [ { "SubdomainName": "client1", "ClientName": "Eastern Region", "ClientAlias": "eastern-region" }, { "SubdomainName": "client1", "ClientName": "City of Knox", "ClientAlias": "knox" }, { "SubdomainName": "client2", "ClientName": "Eastern Region", "ClientAlias": "eastern-region" }, { "SubdomainName": "client2", "ClientName": "City of Knox", "ClientAlias": "knox" } ];
const result = Object.values(relatedSites.reduce((acc, el) => {
acc[el.SubdomainName] = acc[el.SubdomainName] || { title: el.SubdomainName, links: [] };
acc[el.SubdomainName].links.push({ url: `https://${el.SubdomainName}.com/${el.ClientAlias}`, displayText: el.ClientName });
return acc;
}, {}))
console.log(result)
Array of objects - how to group items by property and squash data from each of grouped items into 1
Simply spread the Array#map
result each time:
const restructureArr = () => {
const restructuredArr = arrToRestructure.reduce((prevVal, nextVal) => ({
...prevVal,
[nextVal["y"]]: [
...(prevVal[nextVal["y"]] || []),
...nextVal.data.map((nextVal) => nextVal.label), // fix
]
}) , []);
return restructuredArr;
}
Some enhancements:
const restructureArr = (arrToRestructure = []) =>
arrToRestructure.reduce((acc, { y, data = [] }) => ({
...acc,
[y]: [
...(acc[y] || []),
...data.map(({ label }) => label),
]
}), []);
const arrToRestructure = [
{ x: "test-x1", y: "test-y1", data: [ {id: 1, label: "label-y1-1"}, {id: 2, label: "label-y1-2"} ] },
{ x: "test-x2", y: "test-y1", data: [ {id: 1, label: "label-y1-3"}, {id: 2, label: "label-y1-4"} ] },
{ x: "test-x2", y: "test-y2", data: [ {id: 1, label: "label-y2-1"}, {id: 2, label: "label-y2-2"} ] }
];
console.log(restructureArr(arrToRestructure));
Match and group object properties in javascript
You can use the reduce
method to group the objects.
const data = { "q_A.1": "info", "q_B.1": "info2", "q_C.1": "info3", "q_D.1": "info4", "q_A.2": "information", "q_B.2": "information2", "q_C.2": "information3", "q_D.2": "information4" };
const result = Object.values(
Object.entries(data).reduce(
(acc, [k, v]) => (Object.assign((acc[k.split(".")[1]] ??= {}), { [k]: v }), acc),
{}
)
);
console.log(result);
In an array of objects how to group objects which have same value and include the values that differ
This looks like something reduce()
should be used for.
Use find()
to find in the existing array element based on some condition.
If element exists, push into colors
property of the element.
Else push into the array a new object.
const arr = [
{
name: 'A',
color: 'blue',
},
{
name: 'A',
color: 'purple',
},
{
name: 'B',
color: 'Yellow',
},
{
name: 'B',
color: 'Green',
},
];
let ans = arr.reduce((agg,curr) => {
let found = agg.find((x) => x.name === curr.name);
if(found){
found.colors.push(curr.color);
}
else{
agg.push({
name : curr.name,
colors : [curr.color]
});
}
return agg;
},[]);
console.log(ans);
Group objects from an array of objects based on two or more keys and from an array of dynamically collected values
You can simply use a 'group-by' with a compound key of the specified properties.
Here calling map()
and join()
on your uniqueKeys
array for each iterated object to generate the compound key, then retrieving or initializing the property using logical nullish assignment (??=) before pushing the object to it. The result is the Object.values()
of the grouped object.
const dataSet = [{ lastName: 'Jones', course: 'Standards', gradDate: '12/12/2022' }, { lastName: 'Smith', course: 'Standards', gradDate: '12/12/2022' }, { lastName: 'Martinez', course: 'Maths', gradDate: '12/12/2022' }, { lastName: 'Santiago', course: 'Photography', gradDate: '12/11/2022' }, { lastName: 'Alexi', course: 'Photography', gradDate: '12/11/2022' }];
const uniqueKeys = ['course', 'gradDate'];
const grouped = {};
for (const o of dataSet) {
const key = uniqueKeys.map(k => o[k]).join('_');
(grouped[key] ??= []).push(o);
}
const result = Object.values(grouped)
console.log(result);
Related Topics
Obtain Form Input Fields Using Jquery
Expose Jquery to Real Window Object with Webpack
Getting the Object's Property Name
Avoid Window Jump to Top When Clicking #-Links
".Addeventlistener Is Not a Function" Why Does This Error Occur
Bootstrap V2 Dropdown Navigation Not Working on Mobile Browsers
How to Submit 2 Forms in One Page with a Single Submit Button
How to Tell Google Translate to Not Translate a Section of a Website
How to Conditionally Add Attributes to React Components
Check If Url Scheme Is Supported in JavaScript
Should an Async API Ever Throw Synchronously
What Happens When Using This.Setstate Multiple Times in React Component
How to Apply CSS to the Elements Within an Iframe