Javascript 遍历对象哈希表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10062317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:41:34  来源:igfitidea点击:

Iterating through an object hashtable

javascripthashtable

提问by Nyxynyx

I am trying to use a hashtable so I can select a specific object stored in an array/object. However, I am having a problem looping through an object.

我正在尝试使用哈希表,以便我可以选择存储在数组/对象中的特定对象。但是,我在遍历对象时遇到问题。

var pins= {};
pins[6] = '6';
pins[7] = '7';
pins[8] = '8';

$('#result3').append('<div>Size: ' + Object.size(pins) + '</div>');
for(var i = 0; i < Object.size(pins); i++) {
    $('#result3').append('<div>' + pins[i] + '</div>');
}

JSFiddle: http://jsfiddle.net/7TrSU/

JSFiddlehttp: //jsfiddle.net/7TrSU/

As you can see in TEST 3which uses object pinto store the data, I am getting undefinedwhen looping through the object pin.

正如您所看到的,TEST 3其中使用 objectpin来存储数据,我undefined在遍历 object时得到了pin

What is the correct way for looping through pin?

循环的正确方法是pin什么?

EDIT

编辑

What happens if instead of just pin[6] = '6', I make pin[6] = an object and I want to loop through the all their idproperties? Actual code snippet of what I'm doing...

如果pin[6] = '6'我使 pin[6] = 一个对象而不是仅仅,并且我想遍历它们的所有id属性,会发生什么情况?我正在做的实际代码片段...

for(var i = 0; i < json.length; i++) {
    markerId = json[i].listing_id

    // Place markers on map
    var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
    var marker = new google.maps.Marker({
                listing_id: markerId,
                position: latLng,
                icon: base_url + 'images/template/markers/listing.png',
    });

    markers[markerId] = marker;
}

for(var marker in markers) {
    console.log('marker ID: ' + marker.listing_id);
    mc.addMarker(marker);
}

The console.logabove returns undefined, and if I do console.log(marker)instead, I get the value of marker.listing_id. Sorry I'm getting confused!

console.log上述收益不确定,如果我这样做console.log(marker),而不是,我得到的价值marker.listing_id。对不起,我开始糊涂了!

I managed to get it to work with $.each(markers, function(i, marker){});but why does the for..inabove not work?

我设法让它工作,$.each(markers, function(i, marker){});但为什么for..in上述不起作用?

回答by Raynos

var hash = {}
hash[key] = value

Object.keys(hash).forEach(function (key) { 
    var value = hash[key]
    // iteration code
})

回答by Rob W

Don't use a for(i=0; i<size; i++)loop. Instead, use:

不要使用for(i=0; i<size; i++)循环。相反,使用:

  1. Object.keys(pins)to get a list of properties, and loop through it, or
  2. Use a for ( key_name in pins)in conjunction with Object.hasOwnProperty(to exclude inherit properties) to loop through the properties.
  1. Object.keys(pins)获取属性列表,并循环遍历它,或
  2. 将 afor ( key_name in pins)Object.hasOwnProperty(排除继承属性)结合使用以循环访问属性。

The problem of your third test case is that it reads the values of keys 0, 1 and 2 (instead of 6, 7, 8).

第三个测试用例的问题在于它读取键 0、1 和 2(而不是 6、7、8)的值。

回答by Quentin

Since you are using jQuery:

由于您使用的是 jQuery:

jQuery.each(pins, function (name, value) {
    $('#result3').append('<div>' + name + "=" + value + '</div>');
});

回答by Rory McCrossan

Try this:

尝试这个:

for (var pin in pins) {
    $('#result3').append('<div>' + pin + '</div>');
}

Example fiddle

示例小提琴

回答by pbfy0

function iterate(obj){
    var keys = Object.keys(obj);
    for(i in keys){
        doSomething(obj[keys[i]].id);
    }
}

This iterates over the idof all fields in any object

这将遍历id任何对象中的所有字段

回答by Wouter J

The pinbegins with 6 up to 8, but the for loop loops from 0 up to 3 (the length of the object). You need to loop from 6 up to 6 + the_size_of_the_object. Something like this:

pin具有6到8开始,而是从0到3(该物体的长度)for循环环路。您需要从 6 循环到6 + the_size_of_the_object. 像这样的东西:

for(var i = 6, len = 6 + Object.size(pins); i < len; i++) {
  $('#result3').append('<div>' + pins[i] + '</div>');
}


Or something like this, this is what I like:

或者像这样的东西,这就是我喜欢的:

for( var i = 5; pin = pins[++i]; ) {
  $('#result3').append('<div>' + pin + '</div>');
}