I'm trying to push my variables into this object but not sure how todo this the proper way:
The jsonArray if manually inputted looks like so:
jsonArray = {
"Person 1": ["Address Rd. City", "777 Street Ave. City"],
"Person 2": ["Address2 Rd. City2", "777 Street Ave. City"]
}
I have a loop in which I want to push variables into the jsonArray and i'm trying like so:
var jsonArray = {};
for (var i = 0....my loop, etc)...
jsonArray.push({fullname: [address, destination]});
This doesn't work, jsonArray always shows as {} when i JSON.stringify(jsonArray)
jsonArray — despite the misleading name — is actually an object so try instead
for (...) {
jsonArray[fullname] = [address, destination];
}
push is a method of Array.prototype. So change the first line to
var jsonArray = []; // array literal
Also declare it like
jsonArray = [
["Address Rd. City", "777 Street Ave. City"],
["Address2 Rd. City2", "777 Street Ave. City"]
]; // you can use indexes to get the elements
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With