Deep Flatten JavaScript Object Recursively

后端 未结 14 2181
囚心锁ツ
囚心锁ツ 2021-02-18 20:34

Data:

var data = [
    {
      \"id\": 1,
      \"level\": \"1\",
      \"text\": \"Sammy\",
      \"type\": \"Item\",
      \"items\": [
               


        
14条回答
  •  渐次进展
    2021-02-18 21:21

    I needed to do the same thing, and while solving my issue found a solution to yours using lodash:

    function kids(node) {
        return node.items
            ? [{...node, items: []}, _.map(node.items, kids)]
            : {...node, items: null};
    }
    
    _.flatMapDeep(data, kids);
    

提交回复
热议问题