gpt4 book ai didi

javascript - 打印数组中包含的 Javascript 对象的名称

转载 作者:行者123 更新时间:2023-11-29 16:13:25 25 4
gpt4 key购买 nike

我创建了一个名为 Location 的对象:

function Location(street,city,postal,phone,fax, lat, lon, distance){
this.street = street;
this.city = city;
this.postal = postal;
this.phone = phone;
this.fax = fax;
this.lat = lat;
this.lon = lon;
this.distance = distance;
}

然后构建每个对象:

example1 = new Locaiton('value', 'value', etc)
example2 etc etc

然后将对象存储在一个数组中

var cities = [example1, example2]

我尝试使用 cities[0].constructor.name,但这只会打印出 Location。

如何打印“example1”以便我可以将它与 if 语句中的匹配元素 ID 进行比较?

最佳答案

example1 是对 Location 对象的引用,而 JavaScript 没有做你想做的事情的概念。

如果你真的需要这个名字,我会把它存储在你的对象上。

关于javascript - 打印数组中包含的 Javascript 对象的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21490863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com