Last active
April 5, 2020 23:56
-
-
Save guayom/77b60c5164de4dbf9c001980fb20f7e0 to your computer and use it in GitHub Desktop.
Test ways to group arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"scripts": [], | |
"styles": [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment