从数组中过滤并分配给另一个数组

时间:2016-11-14 11:42:46

标签: angularjs arrays

我必须数组都有相同的长度,但索引是不同的,我想从一个数组中过滤并根据匹配条件分配给另一个数组,如:

if desids.TransferOrderLineItemID== tList.TransferOrderLineItemID
     $scope.tList.SelectedDestinationLocationIdManyToOne = desids.dLocations

这是我的代码和json:

$scope.desids= [{"TransferOrderID":295,"dLocations":"My Second Location","ProductID":385323,"TransferOrderLineItemID":653},{"TransferOrderID":295,"dLocations":"test dsf - Loc 1","ProductID":385315,"TransferOrderLineItemID":654},{"TransferOrderID":295,"dLocations":"My Second Location","ProductID":385190,"TransferOrderLineItemID":655},{"TransferOrderID":295,"dLocations":"test dsf - Loc 1","ProductID":385192,"TransferOrderLineItemID":656}]


$scope.tList=[{"TransferOrderID":295,"InvItemId":79759,"TransferOrderLineItemID":653,"ProductID":385323},
{"TransferOrderID":295,"InvItemId":78689,"TransferOrderLineItemID":655,"ProductID":385190},
{"TransferOrderID":295,"InvItemId":78691,"TransferOrderLineItemID":656,"ProductID":385192},
{"TransferOrderID":295,"InvItemId":79753,"TransferOrderLineItemID":654,"ProductID":385315}]

我在尝试什么:

for (var i = 0; i < $scope.desids.length; i++) {
                                var FilteredProduct = $filter('filter')($scope.desids, { ProductID: $scope.desids[i].ProductID, TransferOrderLineItemID: $scope.desids[i].TransferOrderLineItemID });
                                $scope.tList[i].SelectedDestinationLocationIdManyToOne = FilteredProduct[0].dLocations;
                            }

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
let desids = [{
  "TransferOrderID": 295,
  "dLocations": "My Second Location",
  "ProductID": 385323,
  "TransferOrderLineItemID": 653
}, {
  "TransferOrderID": 295,
  "dLocations": "test dsf - Loc 1",
  "ProductID": 385315,
  "TransferOrderLineItemID": 654
}, {
  "TransferOrderID": 295,
  "dLocations": "My Second Location",
  "ProductID": 385190,
  "TransferOrderLineItemID": 655
}, {
  "TransferOrderID": 295,
  "dLocations": "test dsf - Loc 1",
  "ProductID": 385192,
  "TransferOrderLineItemID": 656
}];


let tList = [{
  "TransferOrderID": 295,
  "InvItemId": 79759,
  "TransferOrderLineItemID": 653,
  "ProductID": 385323
}, {
  "TransferOrderID": 295,
  "InvItemId": 78689,
  "TransferOrderLineItemID": 655,
  "ProductID": 385190
}, {
  "TransferOrderID": 295,
  "InvItemId": 78691,
  "TransferOrderLineItemID": 656,
  "ProductID": 385192
}, {
  "TransferOrderID": 295,
  "InvItemId": 79753,
  "TransferOrderLineItemID": 654,
  "ProductID": 385315
}];

tList.map(function(elem){
    let index = arrayObjectIndexOf(desids, elem.TransferOrderLineItemID, "TransferOrderLineItemID");
    let matchingObject = desids[index];
    elem.SelectedDestinationLocationIdManyToOne = matchingObject.dLocations;
    return elem;
});

//Used to get the index of the object
function arrayObjectIndexOf(myArray, searchTerm, property) {
    for(var i = 0, len = myArray.length; i < len; i++) {
        if (myArray[i][property] === searchTerm) return i;
    }
    return -1;
}

console.log(tList);
&#13;
&#13;
&#13;