11

I have problems with .stringify(), but I think my JavaScript array must be wrong, here's my code:

var questions = new Array();

$('#Valid').hover(function(){
    for (i=0;i < $('.Questions').length;i++){
        questions[i]=new Array();
        questions[i]['numero']=$('.Numero:eq('+i+')').html();
        questions[i]['question']=$('.ItemInput:eq('+i+')').val();
        questions[i]['variable']=$('.VarName:eq('+i+')').val();
    }

    var stringJSON=JSON.stringify(questions)
    alert (stringJSON)
})

The stringJSON var returns :

[[]]

What am I doing wrong?

2
  • 1
    You should use [] instead of new Array(). Also you should always make your loop variable local using the var keyword! Commented Mar 2, 2011 at 13:56
  • 4
    i < $('.Questions').length is bad. You create a new jQuery object every loop. If your elements are descendants of .Questions, use $('.questions').each(callback) Commented Mar 2, 2011 at 14:00

3 Answers 3

22

Arrays have integer keys, not strings.

Use an object instead; objects in JS sort of look like associative arrays:

var questions = new Array();
$('#Valid').hover(function(){
    for (var i=0;i < $('.Questions').length;i++){
        questions[i]={};
        questions[i]['numero']=$('.Numero:eq('+i+')').html();
        questions[i]['question']=$('.ItemInput:eq('+i+')').val();
        questions[i]['variable']=$('.VarName:eq('+i+')').val();
    }

    var stringJSON=JSON.stringify(questions);
    alert(stringJSON);
});

Setting questions[i] to {} is the key.

You can shorten this syntax:

var questions = new Array();
$('#Valid').hover(function(){
    for (var i=0;i < $('.Questions').length;i++){
        questions[i] = {
            numero:   $('.Numero:eq('+i+')').html(),
            question: $('.ItemInput:eq('+i+')').val(),
            variable: $('.VarName:eq('+i+')').val()
        };
    }

    var stringJSON=JSON.stringify(questions);
    alert(stringJSON);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, it seems so obvious now !
@Coronier: No problem. Arrays vs objects in JS are often confused.
this saved my life, literally. THANKS
7

Fix:

Replace questions[i]=new Array(); with questions[i] = {}; (or = new Object(); if you really want this "ugly" syntax).

Explanation:

Arrays in JavaScript are like arrays in languages like C: They only support integer indexes in the interval [0, array.length).

For associative arrays, you use objects which are created using the object literal {}.

Comments

1

First, your "questions" variable appears to be intended as a real array. What you're putting inside the array, however, are not real arrays — they're just objects with properties.

var questions = [];

$('#Valid').hover(function(){
    for (var i=0;i < $('.Questions').length;i++){
        questions[i] = {
          'numero': $('.Numero:eq('+i+')').html(),
          'question': $('.ItemInput:eq('+i+')').val(),
          'variable': $('.VarName:eq('+i+')').val()
        };
    }

    var stringJSON=JSON.stringify(questions)
    alert (stringJSON)
});

(Note that I also added a var for the "i" loop variable - important!)

Now, as to why the thing is coming up empty, well, I suspect it's because $('.Questions') is coming up empty.

Oh and also, this is something you could use the jQuery ".map()" API for:

 $('#Valid').hover(function() {
   questions = $('.Questions').map(function(i, q) {
     return {
       'numero': $('.Numero:eq('+i+')').html(),
       'question': $('.ItemInput:eq('+i+')').val(),
       'variable': $('.VarName:eq('+i+')').val()
     };
   }).get();

   var stringJSON = JSON.stringify(questions);
   alert(stringJSON);
});

That's a little nicer because it gets around the ugly problem of re-evaluating $('.Questions') on every iteration of the loop.

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.