鍍金池/ 問(wèn)答/HTML/ 遞歸刪除,如果數(shù)組為空,就刪除這個(gè)屬性

遞歸刪除,如果數(shù)組為空,就刪除這個(gè)屬性

  1. 刪除一顆遞歸樹(shù),遇到空的subList:[] 就刪除這個(gè)屬性,如果subList不為空,就保留
// 輸入:

[
    {
        "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":[

                        ]
                    }
                ]
            }
        ]
    }
]

// 輸出

[
    {
        "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,
                    }
                ]
            }
        ]
    }
]
回答
編輯回答
懷中人

同樣性質(zhì)的問(wèn)題換個(gè)方式問(wèn)兩遍???
https://segmentfault.com/q/10...
上一個(gè)問(wèn)題給了答案了,這個(gè)問(wèn)題答案依舊一樣啊

function deleteNode(obj) {
  if (typeof obj !== 'object' || obj === null) {
    return obj
  }
  const copy = Array.isArray(obj) ? [] : {};
  Object.keys(obj).forEach(key => {
    if (key === 'subList' && obj[key].length === 0) {
    } else {
      copy[key] = deleteNode(obj[key])
    }
  })
  return copy;
}

題主別這么問(wèn)問(wèn)題了,你結(jié)合兩個(gè)答案,封裝一個(gè) 高階函數(shù)吧

2018年3月13日 07:30
編輯回答
真難過(guò)

老實(shí)的遞歸

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;
  }, []);
}
2017年9月6日 23:34
編輯回答
孤毒
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)
2018年8月1日 12:49