Deep Flatten JavaScript Object Recursively

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

Data:

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


        
14条回答
  •  说谎
    说谎 (楼主)
    2021-02-18 21:19

    another way with recursive reducer function

     _.reduce(data, function reducer(result, val) {
         var items = _.reduce(val.items, reducer, []);
         val.items = _.isArray(val.items) ? [] : val.items;
         return _.concat(result, val, items);
     }, []);
    

提交回复
热议问题