简体   繁体   English

遍历 object 哈希表

[英]Iterating through an object hashtable

I am trying to use a hashtable so I can select a specific object stored in an array/object.我正在尝试使用哈希表,因此我可以 select 存储在数组/对象中的特定 object。 However, I am having a problem looping through an object.但是,我在循环 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/ JSFiddle : http://jsfiddle.net/7TrSU/

As you can see in TEST 3 which uses object pin to store the data, I am getting undefined when looping through the object pin .正如您在使用 object pin存储数据的TEST 3中看到的那样,当循环遍历 object pin时,我变得undefined

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 id properties?如果我不只是pin[6] = '6'而是设置 pin[6] = an object 并且我想遍历它们的所有id属性,会发生什么情况? Actual code snippet of what I'm doing...我正在做的实际代码片段......

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.log above returns undefined, and if I do console.log(marker) instead, I get the value of marker.listing_id .上面的console.log返回未定义的,如果我改为执行console.log(marker) ,我会得到marker.listing_id的值。 Sorry I'm getting confused!抱歉我有点糊涂了!

I managed to get it to work with $.each(markers, function(i, marker){});我设法让它与$.each(markers, function(i, marker){});一起工作but why does the for..in above not work?但为什么上面的for..in不起作用?

var hash = {}
hash[key] = value

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

Don't use a for(i=0; i<size; i++) loop.不要使用for(i=0; i<size; i++)循环。 Instead, use:相反,使用:

  1. Object.keys(pins) to get a list of properties, and loop through it, or Object.keys(pins)获取属性列表,并循环遍历它,或者
  2. Use a for ( key_name in pins) in conjunction with Object.hasOwnProperty (to exclude inherit properties) to loop through the properties.for ( 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)的值。

Since you are using jQuery:由于您使用的是 jQuery:

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

Try this:试试这个:

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

Example fiddle示例小提琴

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

This iterates over the id of all fields in any object这将遍历任何 object 中所有字段的id

The pin begins with 6 up to 8, but the for loop loops from 0 up to 3 (the length of the object). pin从 6 开始到 8,但是 for 循环从 0 到 3(对象的长度)循环。 You need to loop from 6 up to 6 + the_size_of_the_object .您需要从 6 循环到6 + the_size_of_the_object Something like this:像这样:

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>');
}

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

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