使用for循环

时间:2016-05-12 15:27:30

标签: javascript for-loop

我正在使用以下代码片段,我很确定我做错了,这就是为什么它没有返回多个值。我需要专家的意见。

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

1 个答案:

答案 0 :(得分:5)

您可能想要返回一个对象数组:

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