1

I am trying to prepare a list of objects that I am pushing to another service, I have generated a list of objects that need to be sent. For some reason I cannot stringify the array.

This is my code:

var array = [];
for (var i = 1; i < 5; i++) {
    Write("generating person " + i + "<br>");
    var person = {};
    person.name = "Jane " + i;
    array[i] = person;
}
Write("length:" + array.length)
Write("<br><br>==========<br>")
Write(Stringify(array[1]));
Write("<br>")
Write(Stringify(array));
Write("<br><br>==========<br>")

But this is what is returned:

generating person 1
generating person 2
generating person 3
generating person 4
length:4

==========
{"name":"Jane 1"}
System.Collections.Generic.Dictionary`2[System.String,System.Object] 

1 Answer 1

1

Don't know why, but it seems Stringify() can't handle something you completely omit: array[0] - it never has a value assigned.

Start you for loop from i = 0 and it works.

2
  • you're right, this is interesting then... does this mean we cannot associate a key to the element in the array? I have strings that I wanted to use, but if I have to use an integer and start from 0 it means that wont work. Mar 6, 2020 at 17:15
  • 1
    Arrays will always start from 0. Why are you saying that starting from 0 is a problem? i = 0 can be be used to generate 'Jane 1' easily. The other service won't care about indexing in this array Mar 6, 2020 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.