繁体   English   中英

根据条件将一个数组复制到另一个数组

[英]Copy an array to another array on condition

我正在寻找一种基于某种条件将数组列表复制到另一个数组的巧妙方法。 我不想逐个元素地复制。 这也是花费时间,当然还有很多行

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**
            }
        });

有没有什么整洁的方法,我不必在每个元素上加?

您可以使用Array#filter

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

 var $scope = {}, dataSource; $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" }]; dataSource = $scope.leadsDataSource.filter(function (a) { return a.typeName == 'Lead'; }); console.log(dataSource); 

您可以使用本机Array.prototype.filter()方法:

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

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

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

如果您需要的是深拷贝,请使用angular.copy

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM