0

So I have an angular function that aims to grab some basic details from an array of objects, by looking up using an id, passed into the function.

Anyway, the function works - the first time. That is, it successfully returns the customer name of the first object where the contract id's match. However, if there is more than one contract/object that matches, it only returns the first name.

I understand this must be a basic problem with regards to looping, but can't seem to get it to work!

Thanks so much in advance.

Here is the function:

$scope.getCompanyDetailsByContractId = function(contractId) {

    for (var i = $scope.customers.length - 1; i >= 0; i--) {

        if ($scope.customers[i].contracts[i].id == contractId ) {
            return $scope.customers[i].name;
        };

    };;

}

And here is the data structures:

$scope.customers = [{
            id: 1,
            name: "John Inc",
            currentContractLength: 12,
            currentContractValue: 18000,
            startDate: "09/01/2014",
            endDate: "09/01/2015",
            nextRenewalDate: "09/01/2015",
            mrr: 1500,
            contracts: [{
                id: 234,
                status: "won",
                contractNumber: 1,
                value: 18000,
                startDate: "09/01/2014",
                endDate: "09/01/2015",
                nextRenewalDate: "09/01/2014"
            },
            {
                id: 235,
                status: "pending",
                contractNumber: 2,
                value: 18000,
                startDate: "09/01/2015",
                endDate: "09/01/2016",
                nextRenewalDate: "09/01/2016"
            }]

        }, {
            id: 2,
            name: "Peters Company Ltd",
            currentContractLength: 3,
            currentContractValue: 15000,
            startDate: "09/01/2014",
            endDate: "09/01/2015",
            nextRenewalDate: "09/01/2015",
            mrr: 1500,
            contracts: [{
                id: 543654,
                status: "won",
                contractNumber: 1,
                value: 4200,
                startDate: "09/01/2014",
                endDate: "09/01/2015",
                nextRenewalDate: "09/01/2014"
            },
            {
                id: 54354,
                status: "pending",
                contractNumber: 2,
                value: 18000,
                startDate: "09/01/2015",
                endDate: "09/01/2016",
                nextRenewalDate: "09/01/2016"
            }]
        }];

1 Answer 1

1

It seems you should do 2 loops to look in all possibles contracts.

With the following call you are looking only for the contract with index i

$scope.customers[i].contracts[i].id == contractId

Maybe you should do something like

for (var i = $scope.customers.length - 1; i >= 0; i--) {

    for (var j = 0; j < $scope.customers[i].contracts.length; j++) {
        if ($scope.customers[i].contracts[j].id == contractId ) {
            return $scope.customers[i].name;
        };
    }


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