0

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;
}
4
  • 2
    childOrders[abstractOrders,titleOrder]; is not right at all. You can't use two indexes on a JS array. Commented May 12, 2016 at 15:28
  • 1
    yes, maybe do you mean childOrders.push([abstractOrders, titleOrder]) Commented May 12, 2016 at 15:29
  • What exactly did you intend it to return? What exactly does it actually return? Are there any error messages? Commented May 12, 2016 at 15:31
  • It is intended to return numbers under two category abstract and title. Commented May 12, 2016 at 15:56

1 Answer 1

5

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you please update the answer and let me know how can I retrieve these values?
Done. But its pretty general stuff that you can google.
I could not find such stuff. anyways, its javascript

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.