Reputation: 634
I'm trying to use JS to build a JSON array that looks very much like this:
{
"someKey":"someValue",
"lines":{
"1": {
"someKey": "someValue"
},
"2" {
"someKey": "someValue"
},
}
}
Here's my JavaScript:
var myArray = {
someKey: "someValue",
lines: []
};
var count = 0;
$('.class_that_exists_twice').each(function() {
count ++;
myArray.lines[count] = {
someKey: "someValue",
};
});
However, the array that is returned looks wrong. Any ideas what I'm doing wrong? (Please let me know if I should post the array as well)
Thank you very much!
Upvotes: 0
Views: 427
Reputation: 9691
In the first JSON lines is an object. If you want to get your JSON to look like that you could do this:
var myArray = {
someKey: "someValue",
lines: {}
};
$('.class_that_exists_twice').each(function(index, obj) {
myArray.lines[index] = {
someKey: "someValue",
};
});
Fiddle: http://jsfiddle.net/g1pseum6/
Upvotes: 1
Reputation: 1588
What you are trying to create is not an array but an object. So it should be like this:
var myArray = {
someKey: "someValue",
lines: {}
};
The rest looks fine
Upvotes: 1