Recursively delete, if the array is empty, delete this attribute

  1. Delete a recursive tree, delete this attribute if there is an empty subList: [], and keep
if subList is not empty.
// :

[
    {
        "paramId":500061,
        "sourcePath":"Status",
        "weight":100,
        "subList":[

        ]
    },
    {
        "paramId":500045,
        "sourcePath":"Result",
        "weight":100,
        "subList":[
            {
                "paramId":500042,
                "sourcePath":"Partners",
                "weight":100,
                "subList":[
                    {
                        "paramId":500043,
                        "sourcePath":"StockName",
                        "weight":100,
                        "subList":[

                        ]
                    },
                    {
                        "paramId":500027,
                        "sourcePath":"ShoudDate",
                        "weight":100,
                        "subList":[

                        ]
                    }
                ]
            }
        ]
    }
]

/ / output

[
    {
        "paramId":500061,
        "sourcePath":"Status",
        "weight":100,
    },
    {
        "paramId":500045,
        "sourcePath":"Result",
        "weight":100,
        "subList":[
            {
                "paramId":500042,
                "sourcePath":"Partners",
                "weight":100,
                "subList":[
                    {
                        "paramId":500043,
                        "sourcePath":"StockName",
                        "weight":100,
                    },
                    {
                        "paramId":500027,
                        "sourcePath":"ShoudDate",
                        "weight":100,
                    }
                ]
            }
        ]
    }
]
Apr.09,2021

const arr = [
  {
    "paramId":500061,
    "sourcePath":"Status",
    "weight":100,
    "subList":[

    ]
  },
  {
    "paramId":500045,
    "sourcePath":"Result",
    "weight":100,
    "subList":[
      {
        "paramId":500042,
        "sourcePath":"Partners",
        "weight":100,
        "subList":[
          {
            "paramId":500043,
            "sourcePath":"StockName",
            "weight":100,
            "subList":[

            ]
          },
          {
            "paramId":500027,
            "sourcePath":"ShoudDate",
            "weight":100,
            "subList":[

            ]
          }
        ]
      }
    ]
  }
];

const result = JSON.parse(JSON.stringify(arr).replace(/,"subList":\[]/g,''));
console.log(result)


----------


const handel = (item, attr) => (item[attr] && item[attr].length ? item[attr].forEach(row => (handel(row, attr))) : delete item[attr]);
arr.forEach(item => handel(item, 'subList'));
console.log(arr)

questions of the same nature are asked twice in a different way.
https://codeshelper.com/q/10.
has given the answer to the last question, and the answer to this question remains the same


function handleRecur(data) {
  return data.reduce((iter, val) => {
    val = {...val};
    val.subList.length ? val.subList = handleRecur(val.subList) : delete val.subList;
    iter.push(val);
    return iter;
  }, []);
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5b9a5-44998.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5b9a5-44998.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?