我有一个对象数组,我需要通过它来获取数据,而我却没有得到它。我正在使用纯JavaScript。
解决了!!!!!!!
感谢您的个人帮助!数组作为一个字符串输入,我在迭代之前解析,一切都解决了!
// 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
}
}
&#13;
答案 0 :(得分:2)
您尚未声明data
数组
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);
}
使用参数
更新回答。
//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);
}
}
答案 1 :(得分: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)
您需要将对象分配给变量,然后在函数中调用该变量