The JSON returned from my Ajax call looks like this:
returnedData = "[
{ id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London',
orders:
[
{ product: 'TV', price: 599.99, quantity: 2, orderTotal: 1199.98 }
]
}
]";
var customers = JSON.parse (returnedData);
console.log(customers.length); // This prints the length of the string data
When assigning the result directly, it treats it as a string.
var customers = [
{ id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London',
orders:
[
{ product: 'TV', price: 599.99, quantity: 2, orderTotal: 1199.98 }
]
}
];
console.log(customers.length); // This now prints 1, which is the number of objects
Why does this happen? Is there a way to dynamically assign it?