2016-06-16 3 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" 
     }]; 

조건 스크립트

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의 힘입니다. –

3

을 사용할 수 있습니다 여기에

var dataSource = $scope.leadsDataSource.filter(function(value){ 
    return value.typeName == 'Lead'; 
}); 
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;  
    }); 
관련 문제