Miranda Miranda - 19 days ago 7
AngularJS Question

filter from an array and assign to another array

i have to array both have same length but index is different , what i want to filter from one array and assign to another array based on matching condition like:

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


here is my code and 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}]


what i was trying:

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

Answer

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

Comments