简体   繁体   English

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

[英]Print name of Javascript object contained in array

I created an object called Location: 我创建了一个名为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;
}

Then constructed each of the objects: 然后构造每个对象:

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

Then stored the objects in an array 然后将对象存储在数组中

var cities = [example1, example2]

I tried using cities[0].constructor.name , but this just prints out Location. 我尝试使用cities[0].constructor.name ,但这只是打印出位置。

How do I print out "example1" so that I can compare it with a matching element id in an if statement? 如何打印“ example1”,以便可以将其与if语句中匹配的元素ID进行比较?

example1 is a reference to your Location object, and JavaScript has no concept of doing what you want to do. example1是对Location对象的引用,而JavaScript没有执行您想做的事情的概念。

If you really need the name, I would store it on your object. 如果您确实需要名称,我会将其存储在您的对象上。

Objects don't have any back-reference to the variables that refer to them. 对象对引用它们的变量没有任何反向引用。 Consider this: 考虑一下:

var example1 = new Location(...);
var example2 = example1;

Now there's one location named by two variables, which would you expect the "name" to be? 现在有一个由两个变量命名的位置,您希望“名称”是哪个?

May I suggest that rather storing the set of locations as an array, you store it as an object. 我是否可以建议将位置集存储为数组,而是将其存储为对象。 You appear to be trying to store the name of a city in combination with the location data. 您似乎正在尝试将城市名称与位置数据一起存储。

You could use a data structure like this for cities: 您可以对城市使用如下数据结构:

var cities = {
    "london": example1,
    "lisbon": example2
}

You can then do: 然后,您可以执行以下操作:

cities.london.name

You have to add return this to the end of your Location function definition. 你必须添加return this到自己的位置函数定义的结束。

You can't do what you want. 你不能做你想做的。 Objects in JavaScript are generally used by reference rather than by value. JavaScript中的对象通常按引用而不是按值使用。 So for, example, if you used: 例如,如果您使用:

var ex1 = new Location();
var ex2 = ex1;
var ex3 = ex1;

Then all three of those variables ( ex1 , ex2 , and ex3 ) are referring to the exact same object . 然后,所有这三个变量( ex1ex2ex3 )都指向完全相同的对象 There's no way that the object could pick one even if the language tried to permit it. 即使语言试图允许它也无法选择对象。

You could store the objects in another dynamic object, and then use the "variable name" as the key, but I'm not 100% sure that that buys you much. 您可以将对象存储在另一个动态对象中,然后使用“变量名”作为键,但是我不确定100%会给您带来多少好处。 The approach would be: 该方法将是:

var examples = {};
examples["example1"] = new Location('value', 'value', etc);
examples["example2"] = etc etc

At that point, you would be able to access the key associated with the current object. 届时,您将能够访问与当前对象关联的密钥。 The thing is, you would have to have access to the key in order to get to the object, so, like I said, I'm not sure how much it buys you . 问题是,您必须有权访问钥匙才能到达对象,因此,就像我说的那样,我不确定它能为您带来多少收益。 . .

Note: Chris Alexander and Jay are suggesting similar approaches . 注意:克里斯·亚历山大(Chris Alexander)和杰伊(Jay)都建议采用类似的方法。 . .

You're wanting it to print out the name of the variable it would appear. 您希望它打印出将出现的变量的名称。 This is actually not possible. 实际上这是不可能的。

If you need to do this, you have a couple of options. 如果需要执行此操作,则有两种选择。 The two best options are probably: 最好的两个选择是:

1) Add some kind of name function to the Location. 1)在位置中添加某种名称功能。 Then add a "toString()" function which returns the string. 然后添加一个“ toString()”函数,该函数返回字符串。 Then, if you use it in places where it needs to convert it to a string, it'll use whatever the output of toString is. 然后,如果在需要将其转换为字符串的地方使用它,它将使用toString的输出。

2) Instead of storing them in an array, store them in an object where the key is the name you want. 2)而不是将它们存储在数组中,而是将它们存储在键是您想要的名称的对象中。 Then you can use the key instead. 然后,您可以使用密钥。

var locations = {
    example1: new Location(),
    example2: new Location()
}

for (var name in locations) { console.log(name); } // logs "example1" and "example2"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM