1
var menu_items = 0;

$(".trueol").each(function(){

    menu_items = menu_items + 1;

});

var product_price = new Array(menu_items);

var product_name = new Array(menu_items);

        $(".price").each(function(){
        var i = 0;
            product_price[i] = $(this).data("p");
            document.write(product_price[i]); /*Inner document.write*/
            i++;
        });
         document.write(product_price[2]); /*Outer document.write*/

        $(".menu_product").each(function(){
            var j = 0;
            product_name[j] = $(this).text();
            document.write(product_name[j]); /*Inner document.write*/
            j++;
        });

           document.write(product_name[2]); /* Outer document.write */

Here, the inner document.write works just fine but outer ones print undefined. P.S menu_items has a value of 4 after iterating. What's the problem ?

6
  • It's a scope issue. product_name is correctly not defined inside that function. You need to read up on variable scope. :) Commented May 1, 2014 at 8:49
  • 2
    i think u should put var i=0 and var j=0 out of the loop. otherwise it will always save data in array at position 0 no increment Commented May 1, 2014 at 8:50
  • 1
    @i-Conica: the variable is defined, otherwise the op would get a reference error. Commented May 1, 2014 at 8:51
  • Show the complete code... And Fiddle would be better...!! Commented May 1, 2014 at 8:52
  • Yeah I looked again, it's either been edited or it's too early in the morning for me. :) Commented May 1, 2014 at 8:53

3 Answers 3

1

Answers explaining using the each iterator's index are correct, but you're overcomplicating the issue. You can simply push the values onto the end of the arrays and miss out any counting...

var product_price = [];
var product_name = [];

$(".price").each(function(){
    var value = $(this).data("p");
    product_price.push(value);
    document.write(value); /*Inner document.write*/
});
document.write(product_price[2]); /*Outer document.write*/

$(".menu_product").each(function(){
    var value = $(this).text();
    product_name.push(value);
    document.write(value); /*Inner document.write*/
});

document.write(product_name[2]); /* Outer document.write */

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Should you need to know the number of items in the arrays at any point, just use

product_price.length

or

product_name.length

One other hint. I assume you're using document.write as a method of outputting data for debugging purposes. Try using console.log("text"); instead, and then open the browser console (usually F12 and go to the console tab).

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

1 Comment

You're welcome - it's much better to use that than pollute the DOM with something that doesn't need to be there.
1

The problem is you reset your counter at every single iteration (var i = 0 and var j = 0).

Just use .each() with an index.

// Arrays for your values
var product_price = [];
var product_name = [];

$(".price").each(function(index) {
    product_price.push($(this).data("p"));
    document.write(product_price[index]); /*Inner document.write*/
});

$(".menu_product").each(function(index) {
    product_name.push($(this).text());
    document.write(product_name[index]); /*Inner document.write*/
});

// Outer write
document.write(product_name[2]);

Comments

0

Try tro use push() as in every iteration your counter sets to 0,

var product_name = [],
    product_price = [];

$(".price").each(function(){
    t = $(this).data("p");
    document.write(t); /*Inner document.write*/
    product_price.push(t); // use push here
});
document.write(product_price[2]); /*Outer document.write*/

$(".menu_product").each(function(){
     t = $(this).text();
     document.write(t); /*Inner document.write*/
     product_name.push(t); // use push here
});
document.write(product_name[2]); /* Outer document.write */

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.