在条件下将数组复制到另一个数组

时间:2016-06-16 10:19:33

标签: javascript angularjs

我正在寻找一种根据某些条件将数组列表复制到另一个的简洁方法。我不想逐个元素地复制。这也是时间,并且有很多行

MYJSON

$scope.leadsDataSource=[{
            id: 1,
            type: 1,
            typeName: "Lead",
            client: 1,
            clientName: "Ljungbloms Elektriska AB",
            marking: "Marking for Ljungbloms Elektriska AB",
            status: 2,
            statusName: "Open",
            stage: 2,
            stageName: "Stage 2",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        }, {
            id: 2,
            type: 1,
            typeName: "Lead",
            client: 2,
            clientName: "Solina Sweden AB",
            marking: "Marking for Solina Sweden AB",
            status: 1,
            statusName: "Closed",
            stage: 3,
            stageName: "Stage 3",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        }, {
            id: 3,
            type: 2,
            typeName: "Opportunity",
            client: 3,
            clientName: "H & M Hennes & Mauritz GBC AB",
            marking: "Marking for H & M Hennes & Mauritz GBC AB",
            status: 3,
            statusName: "Pending",
            stage: 4,
            stageName: "Stage 4",
            leadValue: 1,
            probability: 1,
            issuer: 1,
            issuerName: "Sales",
            handler: 1,
            handlerName: "Sales",
            created: 1462345200000,
            createdString: "2016-05-04"
        }];

条件脚本

 var dataSource=[];
        angular.forEach($scope.leadsDataSource, function (value, key) {
            if(value.typeName=='Lead'){
                 //**copy the row to dataSource**
            }
        });

有没有任何整洁的方式,我不必推动每个元素?

5 个答案:

答案 0 :(得分:4)

您可以使用Array#filter

SELECT Count(DISTINCT pi.pk_PersonalInfo_ID) AS TotalEmployees, 
       Max(si.ServiceInfoJobStatus)          AS ServiceInfoJobStatus, 
       dsg.NAME                              AS Desgination 
FROM   personalinfo pi 
       INNER JOIN serviceinfo si 
               ON si.fk_PersonalInfo_ServiceInfo_PID = pi.pk_PersonalInfo_ID 
       INNER JOIN setup.designation dsg 
               ON dsg.DesignationID = si.Designation_ID 
WHERE  si.ServiceInfoJobStatus IN ( 1, 3 ) 
GROUP  BY dsg.NAME

var dataSource = $scope.leadsDataSource.filter(function (a) {
        return a.typeName=='Lead';
    });

答案 1 :(得分:3)

您可以使用原生Array.prototype.filter()方法:

https://api.twitter.com/1.1/search/tweets.json?q=from%3Ausername&&include_entities=true&filter=images&page=1

答案 2 :(得分:1)

这是一个(整洁的)解决方案:

       var dataSource = $scope.leadsDataSource.filter(function(item) {
          return item.typeName ==='Lead';
        });

答案 3 :(得分:1)

如果是您需要的深层副本,请使用angular.copy

var dataSource=[];
        angular.forEach($scope.leadsDataSource, function (value, key) {
            if(value.typeName=='Lead'){
                dataSource.push(angular.copy(value))     
            }
        });

答案 4 :(得分:1)

或者你使用旧的经典地图。

   var dataSource = $scope.leadsDataSource.map(function (item) {
        if(item.typeName === 'Lead') return item;    
    });