Best way to store a key=>value array in JavaScript?

前端 未结 7 1896
借酒劲吻你
借酒劲吻你 2020-11-28 00:27

What\'s the best way to store a key=>value array in javascript, and how can that be looped through?

The key of each element should be a tag, such as

7条回答
  •  心在旅途
    2020-11-28 01:20

    If I understood you correctly:

    var hash = {};
    hash['bob'] = 123;
    hash['joe'] = 456;
    
    var sum = 0;
    for (var name in hash) {
        sum += hash[name];
    }
    alert(sum); // 579
    

提交回复
热议问题