1

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
  }	
  
}    

1
  • 1
    var data = <your array> maybe has something to do with the issue? Commented Apr 12, 2017 at 13:19

2 Answers 2

2

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);

  }
}

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry for not clearing up, but I get the data array by parameter in my function, I edited the question
@csf see my updated answer.still same issue is there. you have not declared array . you must declare the array before pass with getArray function
The array is only an example of what is received by the function, this array is not declared in this js only received as parameter. I get the data successfully. I am putting the array just so they can see the format of the data received
@csf ..ok..then how to get the array ?. i m not get your point .please show more code..
I have a jsf button that calls this function and passes the array as param param. No problem in receiving array because when I make console.log (date) I can see the complete array. The problem is in the iteration that returns undefined
|
0
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

2 Comments

The array is only an example of what is received by the function, this array is not declared in this js only received as parameter. I get the data successfully. I am putting the array just so they can see the format of the data received
Thanks for the personal help! The array was entering as a string I parse before iterating and everything is solved!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.