0

This is the script:

for(var i=0;i < data.length; i++)
{
    new_array.push(data[i].catalog_name,data[i].price);
    $("#print_receipt").append(
        "<table><thead><tr><th>Item Name</th><th>Unit Price</th></thead><tbody>");

    for (var j=0;j < new_array.length; j++){
        $.each(new_array[j], function( index, value ) {
            $("#print_receipt tbody").append("<tr><td>"+value+"</td></tr>");
        });                         
    }
    $("#print_receipt").append(value+"</tbody></table>");
}

"+value+" This line actually outputs all the values of both arrays that were pushed into new_array by this line new_array.push(data[i].catalog_name,data[i].price);

That is okay, but I want to be able to call them individually. For instance, in the event I want make a table within this $.each loop, how do I know, which columns takes which array's value?

So, how do I call, value.catalog_name or value.price within $.each instead of just value?

10
  • new_array.push(data[i].catalog_name,data[i].price); <- price is the only item you have access to in new_array. To access other things, you would need to first create an array, and then push that array into new_array. Then the index, value would have more meaning, as the index would actually be useful. Commented Jan 12, 2016 at 1:57
  • @RichardTheobald, I don't quite understand, new_array currently have 2 arrays; namely catalog_name and price Commented Jan 12, 2016 at 1:59
  • try this.catalog_name , this.price Commented Jan 12, 2016 at 2:00
  • Ah, you are correct; my bad. I misread that comma as a period. However, it's pushing two values into the array, the catalog name, and the price. Instead, you would need to do something like: new_array.push({ 'catalog_name': data[i].catalog_name, 'price': data[i].price});...in this case, you're pushing an object into the array rather than pushing two separate values into the array. Commented Jan 12, 2016 at 2:01
  • @RichardTheobald, how do I call it out then? I tried value.price and it says undefined Commented Jan 12, 2016 at 2:04

1 Answer 1

1

The problem is that you are doing things using arrays, and javascript arrays are numeric index only. Instead, make new_array into an object containing objects, and then you are able to retrieve things by name.

var new_array = {};
for(var i=0;i < data.length; i++)
{
    new_array[i] = ({ 'catalog_name':data[i].catalog_name, 'price':data[i].price });
}
$("#print_receipt").append( "<table><thead><tr><th>Item Name</th><th>Unit Price</th></thead><tbody>");
$.each(new_array, function() {
    $("#print_receipt tbody").append("<tr><td>"+this.catalog_name+"</td><td>"+this.price+"</td></tr>");
});
$("#print_receipt").append("</tbody></table>");

https://jsfiddle.net/8weoun7c/

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

4 Comments

that's fantastic, however could help to modify a bit for my case? When I tried I get comma separated value under ctalog_name and price, unlike yours properly breaks into new rows instead of comma.
I don't understand what you mean. Are you saying that new_array is just a comma separated list?
I mean that, values for catalog_name and price are comma separated inside new_array
I would suggest looking closely at the bracketing. Square brackets are for arrays, whereas braces are for objects. Also, slowly go through the jsfiddle and see if you can find where your code differs.

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.