合并AngularJS中的数组数组 [英] Merge an array of arrays in AngularJS

查看:161
本文介绍了合并AngularJS中的数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器,这是堆栈溢出上另一个OP的结果。

I have the following controller which was a result of another OP on Stack Overflow.

var getData = function() {
        var swindon = $http.get('(url)/swi/10?expand=true'),
            paddington = $http.get('(url)/pad/10?expand=true'),
            reading = $http.get('(url)/rdg/10?expand=true');
        $q.all([swindon,paddington,reading]).then(function(arrayOfResults) {
            $scope.listOfServices = arrayOfResults;
            console.log($scope.listOfServices);
        });
    };

  getData();

这会导致类似的结果

我现在想做的是将这三个数组整合为一个数组。这些数组在结构上与存储在trainServices数组中的记录完全相同

What I would like to do NOW is CONCAT these three arrays into ONE array. The arrays are absolutly identical in structure with the records being stored in the trainServices array

我已经尝试过类似

$scope.listOfServices = $scope.listOfServices.data.concat(data);

又返回未定义状态

I可以在嵌套的NG-REPEAT中使用这些数组,但是我需要从三个数组中过滤出重复项,因此我需要将它们作为一个。数据来自实时供稿,因此我无法在数据到达应用程序之前进行查询。

I can use the arrays in a nested NG-REPEAT, but I need to filter out duplicates from the three arrays, so I need them as one. The data is from a live feed so I cannot do the query before the data gets to the app.

这是我目前如何调用数组

This is how I call the arrays at the moment

<tbody ng-repeat="item in listOfServices">
      <tr ng-repeat="service in item.data.trainServices | customDelayFilter">

我还包括了JSON.stringify($ scope.listOfServices)的在线JSON表示形式

I have also included an ONLINE JSON representation of JSON.stringify($scope.listOfServices)

不确定为什么顶层显示JSON。实际上不是输出中的字符串

Not sure why the top level says JSON.. thats not actually a string in the output

推荐答案

您可以尝试:

$scope.listOfServices = [].concat(...arrayOfResults.map(item => item.data.trainServices));

它应该给您 concated trainServices对象的数组

it should give you concated array of trainServices' objects

arrayOfResults.map(item => item.data.trainServices)

在我们的案例中,此代码使用 Array.prototype.map 函数返回三个trainServices数组的数组

this code in our case returns an array of three trainServices arrays, using Array.prototype.map function

...arrayOfResults.map(item => item.data.trainServices)

此代码使用 spread 运算符,它使我们能够将数组数组传递给 Array.prototype.concat 函数,该函数合并那些对象数组

this code uses spread operator, which enables us to pass our array of arrays to Array.prototype.concat function, which merges those arrays of objects

这篇关于合并AngularJS中的数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆