Created
September 13, 2018 17:54
-
-
Save mumairofficial/add12ab14f284380f97b097816171e14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| module.exports = [ | |
| { | |
| id: 1, | |
| label: 'one', | |
| child: [ | |
| { | |
| id: 2, | |
| label: 'two' | |
| }, | |
| { | |
| id: 3, | |
| label: 'three' | |
| } | |
| ] | |
| }, | |
| { | |
| id: 4, | |
| label: 'four', | |
| child: [ | |
| { | |
| id: 5, | |
| label: 'five' | |
| } | |
| ] | |
| }, | |
| { | |
| id: 6, | |
| label: 'six', | |
| child: [ | |
| { | |
| id: 7, | |
| label: 'seven' | |
| }, | |
| { | |
| id: 8, | |
| label: 'eight' | |
| }, | |
| { | |
| id: 9, | |
| label: 'nine' | |
| }, | |
| { | |
| id: 10, | |
| label: 'ten' | |
| } | |
| ] | |
| }, | |
| { | |
| id: 11, | |
| label: 'eleven', | |
| child: [] | |
| } | |
| ]; |
This file contains hidden or 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 rolesObjectArray = require('./data'); | |
| console.log('*** RAW ***') | |
| console.log(JSON.stringify(rolesObjectArray, null, 4)); | |
| console.log('*** RAW END ***') | |
| // role ids from back-end | |
| const userOwnedRoleIds = [1, 5, 2, 4, 6, 9, 10, 11]; | |
| // final result json array to populate menu | |
| const resultRolesObjectArray = []; | |
| // actual meat of code | |
| rolesObjectArray.forEach(parentNode => { | |
| const tempObj = {}; | |
| if (userOwnedRoleIds.indexOf(parentNode.id) >= 0) { | |
| tempObj.id = parentNode.id; | |
| tempObj.label = parentNode.label; | |
| tempObj.child = []; | |
| parentNode.child.forEach(childNode => { | |
| if (userOwnedRoleIds.indexOf(childNode.id) >= 0) { | |
| tempObj.child.push(childNode) | |
| } | |
| }); | |
| resultRolesObjectArray.push(tempObj); | |
| } | |
| }); | |
| console.log("*** RESULT ***"); | |
| console.log(JSON.stringify(resultRolesObjectArray, null, 4)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment