Skip to content

Instantly share code, notes, and snippets.

@guayom
Last active April 5, 2020 23:56
Show Gist options
  • Save guayom/77b60c5164de4dbf9c001980fb20f7e0 to your computer and use it in GitHub Desktop.
Save guayom/77b60c5164de4dbf9c001980fb20f7e0 to your computer and use it in GitHub Desktop.
Test ways to group arrays
{
"scripts": [],
"styles": []
}
const rules = [
{
"group": 0,
"id": "sOHVQWXg9JoEW1TadgnC",
"operator": "more-than-equal-to",
"type": "page-time",
"unit": "second",
"value": "5"
},
{
"group": 1,
"id": "iH35qjoJ9LFZqeNOtDeg",
"operator": "device-on",
"type": "device",
"unit": "0",
"value": "phone"
},
{
"group": 1,
"id": "LBEygyuaaoCqfPPSyx8O",
"operator": "device-on",
"type": "device",
"unit": "0",
"value": "tablet"
},
{
"group": 2,
"id": "nAFaVYftsBhMogM1dki3",
"operator": "contains",
"type": "url-path",
"unit": "0",
"value": "/blog"
}
]
const groupRules = (rules) => {
return rules.reduce((acc, curr) => {
if (!acc[curr.group]) acc[curr.group] = [];
acc[curr.group].push(curr);
return acc;
}, []);
};
const GroupedRules = groupRules(rules);
GroupedRules.map(group => console.log({ group }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment