Home > Blockchain >  How to add item to each level of a nested array
How to add item to each level of a nested array

Time:01-15

I have infinite levels array in each object and I want to add "id" based on levels. Suppose if level 1 then id should be 1 and level two then id will be 2 etc.

{
"name": "Anything2",
"code": "SS_1",
"levels": [
    {
        "levelName": "New level",
        "levels": [
            {
                "levelName": "New Level2",
                "levels": [
                    {
                        "levelName": "New Level2",
                        {
                            "levelName": "New Level2",
                            "levels": [
                                {
                                    "levelName": "New level"
                                }
                            ]
                        }
                    },
                    {
                        "levelName": "New Level2",
                    },
                    {
                        "levelName": "New Level2",
                    }
                ]
            },
            {
                "levelName": "New Level2"
            },
            {
                "levelName": "New Level2",
                "levels": [
                    {
                        "levelName": "New level"
                    }
                ]
            }
        ]
    }
]

}

I want to convert the above array into below new array. I have tried using for loop but its not working I am not getting expected data. I want to add "id" based on levels. Suppose if level 1 then id should be 1 and level two then id will be 2 etc.

{
"name": "Anything2",
"code": "SS_1",
"levels": [
    {
        "level": 1,
        "levelName": "New level",
        "levels": [
            {
                "level": 2,
                "levelName": "New Level2",
                "levels": [
                    {
                        "level": 3,
                        "levelName": "New Level2",
                        {
                            "levelName": "New Level2",
                            "levels": [
                                {
                                    "level": 4,
                                    "levelName": "New level"
                                }
                            ]
                        }
                    },
                    {
                        "level": 3,
                        "levelName": "New Level2",
                    },
                    {
                        "level": 3,
                        "levelName": "New Level2",
                    }
                ]
            },
            {
                "level": 2,
                "levelName": "New Level2"
            },
            {
                "level": 2,
                "levelName": "New Level2",
                "levels": [
                    {
                        "level": 3,
                        "levelName": "New level"
                    }
                ]
            }
        ]
    }
]

}

CodePudding user response:

You could take a recursive approach and hand over an incremented level for each level.

addLevels takes a level variable and returns a nother function which separates levels from the object. The rest of the object is a new variable.

The inner function returns a new object with a level property, the old object without levels and a property levels which gets the mapping of the nested arrays.

addLevel features a closure over level which keeps the value for the nested function.

const
    addLevel = (level = 0) => ({ levels = [], ...o }) =>
        ({ level, ...o, levels: levels.map(addLevel(level   1)) }),
    data = { name: "Anything2", code: "SS_1", levels: [{ levelName: "New level", levels: [{ levelName: "New Level2", levels: [{ levelName: "New Level2" }, { levelName: "New Level2", levels: [{ levelName: "New level" }] }, { levelName: "New Level2" }, { levelName: "New Level2" }] }, { levelName: "New Level2" }, { levelName: "New Level2", levels: [{ levelName: "New level" }] }] }] },
    result = addLevel()(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

Here I call the property "depth":

const data = {
  "name": "Anything2",
  "code": "SS_1",
  "levels": [{
    "levelName": "New level",
    "levels": [{
        "levelName": "New Level2",
        "levels": [{
            "levelName": "New Level2"
          },
          {
            "levelName": "New Level2",
            "levels": [{
              "levelName": "New level"
            }]
          },
          {
            "levelName": "New Level2"
          },
          {
            "levelName": "New Level2"
          }
        ]
      },
      {
        "levelName": "New Level2"
      },
      {
        "levelName": "New Level2",
        "levels": [{
          "levelName": "New level"
        }]
      }
    ]
  }]
};

function addleveldepth(arr, depth = 1) {
  arr.forEach(obj => {
    obj.depth = depth;
    if (obj.levels) {
      addleveldepth(obj.levels, depth   1);
    }
  });
}

addleveldepth(data.levels);

console.log(data);

CodePudding user response:

Think it works:

const data = {"name": "Anything2","code": "SS_1","levels": [{"levelName": "New level","levels": [{"levelName": "New Level2","levels": [{"levelName": "New Level2","levels": [{"levelName": "New level"}]},{"levelName": "New Level2",},{"levelName": "New Level2",}]},{"levelName": "New Level2"},{"levelName": "New Level2","levels": [{"levelName": "New level"}]}]}]};
        

const iter = (arr, level) => arr.map((obj) => Array.isArray(obj.levels) 
      ? { level, ...obj,  levels: iter(obj.levels, level   1) } 
      : { level, ...obj });

const result = {...data, levels: iter(data.levels, 1) };

console.dir(result,  {depth: null})
.as-console-wrapper{min-height: 100%!important; top: 0}

  •  Tags:  
  • Related