AQ Dev
AQ Dev

Reputation: 43

Return more than one values from a function in Javascript using for loop

I am using following code snippet and I am pretty sure I am doing something that wrong that is why it is not returning more than one values. I need experts opinion on it.

function returnValues(testArray)
{
    var accountId, orders, abstractOrders, titleOrder;

    var childOrders = new Array();
    for(var i = 0; i < testArray.length; i++)
    {
        accountId = typeof testArray[i] === 'undefined'?'':testArray[i].id;

        orders = getOrderofParentAccount(accountId);
            abstractOrders = abstractOrderYTD(orders);
            titleOrder = titleOrderYTD(orders);

            childOrders[abstractOrders,titleOrder];
    }

    return childOrders;
}

Upvotes: 0

Views: 64

Answers (1)

Dan
Dan

Reputation: 11084

You probably want to return an array of objects:

function returnValues(testArray)
{
var accountId, orders, abstractOrders, titleOrder;

var childOrders = new Array();
for(var i = 0; i < testArray.length; i++)
{
    accountId = typeof testArray[i] === 'undefined'?'':testArray[i].id;

    orders = getOrderofParentAccount(accountId);
        abstractOrders = abstractOrderYTD(orders);
        titleOrder = titleOrderYTD(orders);

        childOrders.push({abstract: abstractOrders,title: titleOrder}); //<-Changed
}

return childOrders;
}

//To retrieve the values
var orders = returnValues(yourarray);
for( var i in orders ){
    console.log("====="+i+"======");
    console.log('Abstract Orders:');
    console.log(orders[i].abstract);
    console.log('Title Orders:');
    console.log(orders[i].title);
}

Upvotes: 5

Related Questions