2016-06-16 7 views
0

何らかの条件に基づいてアレイリストを別のアレイにコピーする方法を探しています。私は要素ごとに要素をコピーしたくない。このラインアレイを別のアレイにコピーする条件で

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" 
     }]; 

条件スクリプトの多くとofcourseのも要した時間である、と

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

私はそれぞれをプッシュする必要がいけない任意のきちんとした方法があります素子??

答えて

4

あなたはネイティブArray.prototype.filter()メソッドを使用することができます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);

+2

'にSyntaxError:予期しないトークン{' – hsz

+0

は、IDEの電源のthats。 –

3

を使用することができます。ここでは

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

を1(ニート)ソリューションです。

その後、
 var dataSource = $scope.leadsDataSource.filter(function(item) { 
      return item.typeName ==='Lead'; 
     }); 
1

それはあなたが必要深いコピーである場合は、angular.copy

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

それとも古い古典的なマップを使用してください。

var dataSource = $scope.leadsDataSource.map(function (item) { 
     if(item.typeName === 'Lead') return item;  
    }); 
関連する問題