0

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;
                            }
2
  • This is not clear Commented Nov 14, 2016 at 11:44
  • @Weedoze i have to match the TransferOrderLineItemID from both array , based on that TransferOrderLineItemID i have to fill dLocations value of desids into SelectedDestinationLocationIdManyToOne Commented Nov 14, 2016 at 11:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.