遍历javascript中的地图 [英] iterate through a map in javascript

查看:53
本文介绍了遍历javascript中的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的结构:

var myMap = {
    partnr1: ['modelA', 'modelB', 'modelC'],
    partnr2: ['modelA', 'modelB', 'modelC']
};

我将遍历每个元素(partnr)及其关联(模型).

I am going to iterate through each of the elements (partnr) with their associatives (models).

为了达到这个目的,我正在尝试两次$ each迭代,但是什么也没有发生:

I am trying a double $each iteration in order to achieve this, but nothing happens:

$.each(myMap, function (i, val) {
    $.each(i, function (innerKey, innerValue) {

        setTimeout(function () {
            $('#variant').fadeOut("slow", function () {
                $(this).text(innerKey + "-" + innerValue).fadeIn("slow");

            });

        }, i * 6000);

    });
});

当我使用单个值数组(对象)时,我试图实现的淡入淡出效果很好,但在像这样的每个键都需要多个值的情况下,效果不是很好.

The effect with fading in and out that I am trying to achieve is working fine when using a single value array (Object), but not when I need to have more than one value for each key like here.

关于如何成功完成此迭代的任何想法,还有没有其他方法可以使用地图来解决这种情况?

Any ideas of how to accomplish this iteration succesfully and are there other ways than using a map that would be better in this case ?

任何建议都很有趣.

推荐答案

我将使用标准javascript:

I'd use standard javascript:

for (var m in myMap){
    for (var i=0;i<myMap[m].length;i++){
    ... do something with myMap[m][i] ...
    }
} 

请注意处理对象和数组的不同方法.

Note the different ways of treating objects and arrays.

这篇关于遍历javascript中的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆