Reputation: 189
I have an array of objects and I need to go through it to get the data and I'm not getting it. I am working with pure javascript.
Solved !!!!!!!
Thanks for the personal help! The array was entering as a string I parse before iterating and everything is solved!
// Example of array received by the function, this array is not declared in this
// js only received as parameter. I am putting so that they can see the format
// of the data received
[{
"id": 171659,
"latitude": "-51.195946",
"longitude": "-30.021810",
"estado": "INSTALADO"
},
{
"id": 171658,
"latitude": "-51.196155",
"longitude": "-30.021615",
"estado": "INSTALADO"
}
]
// My js file contains only the function that receives the data, it follows the
// complete file. The array is not declared here, just received by the function.
// Received successfully but can not iterate
// ====== Get Array ======
function getArray(data) {
var json = JSON.parse(data); //The data enters as string was needed parse()
for (var i = 0; i < json.length; i++) {
console.log(json[i].id); // undefined
}
}
Upvotes: 1
Views: 96
Reputation: 419
var data = [
{
"id": 171659,
"latitude": "-51.195946",
"longitude": "-30.021810",
"estado": "INSTALADO"
},
{
"id": 171658,
"latitude": "-51.196155",
"longitude": "-30.021615",
"estado": "INSTALADO"
}
]
function getArray(data){
for (var i = 0; i < data.length; i++) {
console.log(data[i].id);
}}
getArray(data)
you need to assign your object in to variable and then call that variable in your function
Upvotes: 0
Reputation: 22500
You have not declared the data
array
var data = [{
"id": 171659,
"latitude": "-51.195946",
"longitude": "-30.021810",
"estado": "INSTALADO"
},
{
"id": 171658,
"latitude": "-51.196155",
"longitude": "-30.021615",
"estado": "INSTALADO"
}
]
for (var i = 0; i < data.length; i++) {
console.log(data[i].id);
}
updated answer using parameter.
//The received array
var pass = [{
"id": 171659,
"latitude": "-51.195946",
"longitude": "-30.021810",
"estado": "INSTALADO"
},
{
"id": 171658,
"latitude": "-51.196155",
"longitude": "-30.021615",
"estado": "INSTALADO"
}
]
getArray(pass);
//My function receiving date
function getArray(data){
for (var i = 0; i < data.length; i++) {
console.log(data[i].id);
}
}
Upvotes: 2