Angularjs Array具有子项的数组的单个维度

时间:2017-05-18 21:39:17

标签: javascript angularjs parent-child

大家好,感谢您的帮助。

我在Angularjs中有以下安排

    $scope.arr = [
   {
      "id":284,
      "name":"Ferreterias",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-02T00:26:00.710Z",
      "updated_at":"2017-05-16T22:24:33.715Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":500,
      "nygfeatures":500,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/284.json",
      "items":{}
   },
   {
      "id":285,
      "name":"Ferre clon 1",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-02T00:27:06.699Z",
      "updated_at":"2017-03-02T00:27:06.699Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":500,
      "nygfeatures":500,
      "is_wms":false,
      "folder_id":null,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":284,
      "origin_label":"Origen: Ferreterias",
      "url":"http://192.168.20.136:3000/layers/285.json",
      "items":{}
   },
   {
      "id":301,
      "name":"Folder 1",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-14T20:17:53.170Z",
      "updated_at":"2017-05-16T23:21:49.803Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":303,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/301.json",
      "items":{}
   },
   {
      "id":302,
      "name":"No se porque",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-03-21T23:01:21.399Z",
      "updated_at":"2017-05-10T17:11:03.255Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/302.json",
      "items":{}
   },
   {
      "id":303,
      "name":"Folder 2",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-21T23:30:59.258Z",
      "updated_at":"2017-05-16T23:26:16.367Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/303.json",
      "items":{}
   },
   {
      "id":308,
      "name":"Capa xxx",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:13:52.971Z",
      "updated_at":"2017-05-16T23:46:54.235Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":335,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/308.json",
      "items":{}
   },
   {
      "id":309,
      "name":"qqqqqqqqqqqqq",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:14:28.789Z",
      "updated_at":"2017-05-17T16:45:55.932Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/309.json",
      "items":{}
   },
   {
      "id":310,
      "name":"dede",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:16:00.119Z",
      "updated_at":"2017-05-16T23:47:13.593Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":308,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/310.json",
      "items":{}
   },
   {
      "id":311,
      "name":"dedede",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:21:56.661Z",
      "updated_at":"2017-05-16T21:44:27.179Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/311.json",
      "items":{}
   },
   {
      "id":312,
      "name":"ferres",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-10T17:33:21.328Z",
      "updated_at":"2017-05-16T20:46:04.872Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":4770,
      "nygfeatures":4770,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/312.json",
      "items":{}
   },
   {
      "id":330,
      "name":"2222222",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-16T19:50:40.220Z",
      "updated_at":"2017-05-16T23:46:56.712Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":308,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":308,
      "origin_label":"Origen: Capa xxx",
      "url":"http://192.168.20.136:3000/layers/330.json",
      "items":{}
   },
   {
      "id":333,
      "name":"ferres 2",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-16T19:59:04.588Z",
      "updated_at":"2017-05-16T22:23:54.274Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":500,
      "nygfeatures":500,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":284,
      "origin_label":"Origen: Ferreterias",
      "url":"http://192.168.20.136:3000/layers/333.json",
      "items":{}
   },
   {
      "id":334,
      "name":"Folder 3",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-16T23:22:51.271Z",
      "updated_at":"2017-05-16T23:26:21.513Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/334.json",
      "items":{}
   },
   {
      "id":335,
      "name":"Folder 4",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-16T23:22:58.654Z",
      "updated_at":"2017-05-16T23:23:02.153Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":334,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/335.json",
      "items":{}
   }
]

我需要将它嵌套在他们的地方。等于" folder_id"和" origin_id"

我尝试使用:

    $scope.arr.map(function(el){
      el.items = $scope.arr.filter(function(item){
        return item.folder_id === el.id
      });
      return el;
    }).filter(function(item){
      return item.folder_id === null;
    })

但是控制台给了我以下错误并停止工作。

TypeError: Converting circular structure to JSON

请帮助!!

2 个答案:

答案 0 :(得分:0)

分配给一些新阵列

$scope.nestArray = $scope.arr.map(function(el){
      el.items = $scope.arr.filter(function(item){
        return item.folder_id === el.id
      });
      return el;
    }).filter(function(item){
      return item.folder_id === null;
    })

console.log($scope.nestArray);



var myApp = angular.module('myApp',[]);
function LoginController($scope) {
     $scope.name = 'Superhero';
    
    $scope.arr = [
   {
      "id":284,
      "name":"Ferreterias",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-02T00:26:00.710Z",
      "updated_at":"2017-05-16T22:24:33.715Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":500,
      "nygfeatures":500,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/284.json",
      "items":{}
   },
   {
      "id":285,
      "name":"Ferre clon 1",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-02T00:27:06.699Z",
      "updated_at":"2017-03-02T00:27:06.699Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":500,
      "nygfeatures":500,
      "is_wms":false,
      "folder_id":null,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":284,
      "origin_label":"Origen: Ferreterias",
      "url":"http://192.168.20.136:3000/layers/285.json",
      "items":{}
   },
   {
      "id":301,
      "name":"Folder 1",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-14T20:17:53.170Z",
      "updated_at":"2017-05-16T23:21:49.803Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":303,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/301.json",
      "items":{}
   },
   {
      "id":302,
      "name":"No se porque",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-03-21T23:01:21.399Z",
      "updated_at":"2017-05-10T17:11:03.255Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/302.json",
      "items":{}
   },
   {
      "id":303,
      "name":"Folder 2",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-03-21T23:30:59.258Z",
      "updated_at":"2017-05-16T23:26:16.367Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/303.json",
      "items":{}
   },
   {
      "id":308,
      "name":"Capa xxx",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:13:52.971Z",
      "updated_at":"2017-05-16T23:46:54.235Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":335,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/308.json",
      "items":{}
   },
   {
      "id":309,
      "name":"qqqqqqqqqqqqq",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:14:28.789Z",
      "updated_at":"2017-05-17T16:45:55.932Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/309.json",
      "items":{}
   },
   {
      "id":310,
      "name":"dede",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:16:00.119Z",
      "updated_at":"2017-05-16T23:47:13.593Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":308,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/310.json",
      "items":{}
   },
   {
      "id":311,
      "name":"dedede",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-10T17:21:56.661Z",
      "updated_at":"2017-05-16T21:44:27.179Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/311.json",
      "items":{}
   },
   {
      "id":312,
      "name":"ferres",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-10T17:33:21.328Z",
      "updated_at":"2017-05-16T20:46:04.872Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":4770,
      "nygfeatures":4770,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/312.json",
      "items":{}
   },
   {
      "id":330,
      "name":"2222222",
      "geometry_type":null,
      "isocrona":false,
      "published":null,
      "created_at":"2017-05-16T19:50:40.220Z",
      "updated_at":"2017-05-16T23:46:56.712Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":308,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":308,
      "origin_label":"Origen: Capa xxx",
      "url":"http://192.168.20.136:3000/layers/330.json",
      "items":{}
   },
   {
      "id":333,
      "name":"ferres 2",
      "geometry_type":"POINT",
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-16T19:59:04.588Z",
      "updated_at":"2017-05-16T22:23:54.274Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":500,
      "nygfeatures":500,
      "is_wms":false,
      "folder_id":301,
      "is_folder":false,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":284,
      "origin_label":"Origen: Ferreterias",
      "url":"http://192.168.20.136:3000/layers/333.json",
      "items":{}
   },
   {
      "id":334,
      "name":"Folder 3",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-16T23:22:51.271Z",
      "updated_at":"2017-05-16T23:26:21.513Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":301,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/334.json",
      "items":{}
   },
   {
      "id":335,
      "name":"Folder 4",
      "geometry_type":null,
      "isocrona":null,
      "published":null,
      "created_at":"2017-05-16T23:22:58.654Z",
      "updated_at":"2017-05-16T23:23:02.153Z",
      "cant_features":0,
      "country":null,
      "totalfeatures":0,
      "nygfeatures":0,
      "is_wms":false,
      "folder_id":334,
      "is_folder":true,
      "filters":null,
      "processing_status":"active",
      "wms_legend":null,
      "origin_id":null,
      "origin_label":"",
      "url":"http://192.168.20.136:3000/layers/335.json",
      "items":{}
   }
]

$scope.nestArray = $scope.arr.map(function(el){
      el.items = $scope.arr.filter(function(item){
        return item.folder_id === el.id
      });
      return el;
    }).filter(function(item){
      return item.folder_id === null;
    })
    
console.log($scope.nestArray);
}

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.1/angular.min.js"></script>


<div ng-app ng-controller="LoginController">
    <div>{{nestArray}}</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

解决!!

    arr_f = $scope.arr.map(function(el){
          el.items = [];
          var items = [];
          $scope.arr.filter(function(item){
            if(item.folder_id == el.id){
              items.push(item);          
            }else if(item.origin_id == el.id){
              items.push(item);          
            }
          });
          el.items = items;
          return el;
        });

    var cant = arr_f.length;

    while(cant--){
      if(arr_f[cant].origin_id != null){
        arr_f.splice(cant,1);
      }else if(arr_f[cant].folder_id != null){
        arr_f.splice(cant,1);
      }
    }

    console.log(arr_f);