0

I'm trying to JSON.stringify an associatif array in javascript, when i console.log it the output is :

Array[0]
adresse:
"zfzf"
adresse2:
"zfz"
adresse3:
"fzfz"
code_postal
"fzfz"
[...]
length:
0
[...]
__proto__:
Array[0]

as you can see the lenght is equal to 0, so when i JSON.Strigify it, the answer is [ ]

i use to build my array like this :

var info = [];
    $("[data-change]").each(function(key,obj){
        if ($(obj).val() == "") {
            i = 1;
        }
        else {
            info[$(obj).attr("name")] = $(obj).val();
            j++;
        }
    });

all those "data-change" are inputs, i've tryied to use the variable j instead of $(obj).attr("name")] and it works but i neeed the key to be the name of the input

  • Make it javascript object. Use `info = {};`, then create your object and then stringify it. You're at this point is creating an array, which don't have `$(obj).attr("name")]` index. That's why it is not working. – Shubham Mar 23 '17 at 10:45

1 Answers1

0

Because Objects doesn't have length property.

Use Object.keys(obj).length to view length

oboshto
  • 3,478
  • 4
  • 25
  • 24