简体   繁体   English

调用javascript对象值会给我“未定义”

[英]Calling javascript object value gives me 'undefined'

I'm working on a google map application and I want some markers to load from a db and display them on a map. 我正在使用Google Map应用程序,我希望从数据库中加载一些标记并将其显示在地图上。 So I created an array and defined an object like this. 因此,我创建了一个数组并定义了这样的对象。

function shop_data(name,latitude,longitude,type)
    {
    this.name=name;
    this.latitude=latitude;
    this.longitude=longitude;
    this.type=type;
    }

Then I added some data retrieved from an ajax request to an array of 'shop_data' objects. 然后,我将从ajax请求中检索到的一些数据添加到“ shop_data”对象数组中。 The array is defined as 'all_shops'. 该数组定义为“ all_shops”。

var i=0;
for(var o in data){
var name = data[o].name;
    var type = data[o].type;
var lat = data[o].lat;
var lng = data[o].lng;

all_shops[i]=new shop_data(name,lat,lng,type);
i++
}

Then when i call 'all_shops[i].name', it gives me the name as it is. 然后,当我调用“ all_shops [i] .name”时,它给了我自己的名字。 But when I call 'all_shops[i].lat' or 'all_shops[i].lng' it gives me 'undefined'. 但是当我叫“ all_shops [i] .lat”或“ all_shops [i] .lng”时,它给了我“未定义”。 I tried parsing the lat,lng too. 我也尝试解析经纬度。 Here lat, lng are double variables in MySQL database. 此处lat,lng是MySQL数据库中的双变量。 I can get the lat,lng values without a problem from the received ajax data by calling, just name,type,lat,lng. 我可以通过调用name,type,lat,lng从接收到的ajax数据中毫无问题地获取lat,lng值。 But things go wrong when i put them all in an array and , call them from the array. 但是,当我将它们全部放入一个数组并从数组中调用它们时,事情就出错了。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Change from

this.latitude=latitude;
this.longitude=longitude;

to

this.lat=latitude;
this.lng=longitude;

You have a simple copy/paste error. 您有一个简单的复制/粘贴错误。

You should call your latitude or longitude from all_shops[i] not lat or lng as you have your shop_data function definition like this 你应该打电话给你的latitudelongitudeall_shops[i]latlng ,你有你的shop_data函数定义是这样

function shop_data(name,latitude,longitude,type)
{
   this.name=name;
   this.latitude=latitude;
   this.longitude=longitude;
   this.type=type;
}

or if want to use lat or lng then change shop_data function definition like 或者如果要使用latlng则更改shop_data函数定义,例如

function shop_data(name,latitude,longitude,type)
{
   this.name=name;
   this.lat=latitude;
   this.lng=longitude;
   this.type=type;
}

Changing variable names to match and a little cleanup gives: 更改变量名称以使其匹配并进行一些清除可以得到:

function shop_data(name,latitude,longitude,type){
    this.name=name;
    this.lat=latitude;
    this.lng=longitude;
    this.type=type;
}

and: 和:

var i=0;
for(var o in data){
    var name = data[o].name;
    var type = data[o].type;
    var lat = data[o].lat;
    var lng = data[o].lng;

    all_shops[i]=new shop_data(name,lat,lng,type);
    i++
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FOR 循环和字符串与 JavaScript 连接给了我一个未定义的值 - FOR loop and string concatenating with JavaScript gives me an undefined value 我得到承诺{ <pending> }作为返回值,并在异步作用域中调用,立即给我未定义的信息 - I get Promise { <pending> } as returned value and also calling in async scope gives me undefined immediately 出乎意料,价值给了我一个未定义的答案 - Unexpected , value gives me an undefined answear 为什么即使我以字符串形式将密钥传递给对象给了我正确的值,我的密钥也会给出未定义的值 - Why does my key give an undefined value even if me passing the key to the object in the form of a string gives me the correct value 使用javascript解析JSON给我未定义,同时从json响应中获取值 - parsing JSON using javascript gives me undefined while getting the value from the json response 使用未定义的值调用javascript函数 - Calling javascript function with undefined value JavaScript object 值未定义 - JavaScript object value undefined Javascript对象属性是可访问的,但给出未定义的错误 - Javascript object attribute is accessible but gives undefined error JavaScript 从 JSON 数组中给出未定义的值 - JavaScript gives undefined value from JSON array 新的File()在javascript nodejs中给了我未定义的文件 - new File() gives me undefined File in javascript nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM