Deep Flatten JavaScript Object Recursively

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

Data:

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


        
14条回答
  •  Happy的楠姐
    2021-02-18 21:18

    Using _.flatMapDeep (available since Lodash 4.7):

    var flatten = function(item) {
      return [item, _.flatMapDeep(item.items, flatten)];
    }
    
    var result = _.flatMapDeep(data, flatten);
    

提交回复
热议问题