-2

I have an array of user's permission:

var permission = ["view_admin", "view_config", ...]; And an array which contains user's menu:

var items = [
    { title: 'a' },
    { title: 'b', rules: 'view_admin', sub: [
        { title: 'ba', rules: 'view_admin1' }
    ] },
    { title: 'c', sub: [
        { title: 'ca', rules: 'view_config', sub: [
            { title: 'caa', rules: 'view_config1' },
            { title: 'cba' }
        ] }
    ] },
    { title: 'd', rules: 'view_other'}
];

I need to :

  • If array's object not contains key "rules" maintain the item
  • If rules isn't in user's permission, delete the object
  • If the rules is into sub and length is equal 0 remove entire sub, therefore remove the item in the sub

So in this case I need to have:

var items = [
    { title: 'a' },
    { title: 'b', rules: 'view_admin'},
    { title: 'c', sub: [
        { title: 'ca', rules: 'view_config', sub: [
            { title: 'cba' }
        ] }
    ] }
];

PS: I don't know number of "sub" that might be have...

This is my tried code but not work

var organizeMenu = function(items, permissions) {
    for (var i = 0; i < items.length; i++) {
        var title = items[i].title;
        console.log(title);
        if (items[i].rules && items[i].rules != '*') {
            if (permissions.indexOf(items[i].rules) < 0) {
                console.log("deleted");
                delete items[i];
            } else {
                if (items[i].submenu) {
                    organizeMenu(items[i].submenu, permissions);
                }
            }
        } 
    }
    return items;
};
Davide
  • 475
  • 4
  • 19
  • 5
    Have you written any code to achieve it? We'd need to see it. – Kyll Jul 06 '15 at 08:02
  • This is what I have... What do you need? – Davide Jul 06 '15 at 08:24
  • Well, SO is not a "do my codes for me" website. We're here to answer programming questions. If you want to do something, then try to do it and come back to us when you stumble upon an issue you can't solve. – Kyll Jul 06 '15 at 08:27
  • See my tried code, but not work it... – Davide Jul 06 '15 at 08:36
  • When posting a code that doesn't work, you also need to specify what didn't work. Does it crash? Does it not produce the output you want? Please read the [help]. – Kyll Jul 06 '15 at 08:37
  • How this items object get formed?Is there any functionality which doing this?or it is from DB or xml file? – RIYAJ KHAN Jul 06 '15 at 11:52

2 Answers2

0

I clearly don't want to write code for you. Nevertheless I will write pseudocode to help you.

Define the function, say cleanItems(array)

var cleanItems = function(array){
//You need to iterate through all the items 


 for(until array.lenth){
      condition1// compare if the rules don't match use permission.indexOf(ruleName)
   recursively call clean items if the sub property exists
   //arrayItem.sub = cleanItems(arrayItem.sub)

   condition// delete if the object is empty 

read more here about finding empty obect

   }
 return the processed array;
}
Community
  • 1
  • 1
Sunil B N
  • 4,159
  • 1
  • 31
  • 52
0

Based on object your given object structure, here what I have tried.

Please check it.

var permission = ["view_admin", "view_config"];

var items = [
    { title: 'a' },
    { title: 'b', rules: 'view_admin', sub: [
        { title: 'ba', rules: 'view_admin1' }
    ] },
    { title: 'c', sub: [
        { title: 'ca', rules: 'view_config', sub: [
            { title: 'caa', rules: 'view_config1' },
            { title: 'cba' }
        ] }
    ] },
    { title: 'd', rules: 'view_other'}
];

function checkInnerObject(arrItems){

    arrItems.forEach(function(objOne,key){

        if(objOne.hasOwnProperty("rules") || objOne.hasOwnProperty("sub")){
            if(permission.indexOf(objOne.rules)>=0){
                  checkInnerObject(objOne.sub);
                  if(objOne.sub.length==0){
                    delete objOne.sub;
                  }
            }else if(objOne.hasOwnProperty("sub")){
                checkInnerObject(objOne.sub);
            } else{  
                    arrItems.splice(key,1);
            }
        }
    })
}

checkInnerObject(items);

console.log(items)
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53