Key-value pair object in Javascript

近期看到同事代码有使用Object作为键值对保存数据, 类似于

var categoryCount = {};
categorys.map(category => {
    // every category total logic
    categoryCount[category] = total;
});
console.log(
categoryCount);

上面的Code完全能正常工作,使用Js中的Map会显得更专业。

var myMap = new Map([
  ['server-hardware', 'server-hardware'],
  ['server-profiles', 'server-profiles'],
  ['enclosures', 'enclosures'],
  ['storage-pools', 'storage-pools'],
  ['storage-systems', 'storage-systems'],
  ['storage-volumes', 'storage-volumes'],
  ['san-managers', 'fc-sans/device-managers'],
  ['managed-sans', 'fc-sans/managed-sans']
]);

var keyString = 'a string',
    keyObj = {},
    keyFunc = function() {};

// setting the values
myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, 'value associated with keyObj');
myMap.set(keyFunc, 'value associated with keyFunc');
myMap.set(NaN, 'not a number');
myMap.set(undefined, 'undefined value');

// getting the values
myMap.get(keyString);    // "value associated with 'a string'"
myMap.get(keyObj);       // "value associated with keyObj"
myMap.get(keyFunc);      // "value associated with keyFunc"

myMap.get('a string');   // "value associated with 'a string'"
                         // because keyString === 'a string'
myMap.get({});           // undefined, because keyObj !== {}
myMap.get(function() {}) // undefined, because keyFunc !== function () {}

var mapIter = myMap.entries();
let item;
 do {
   item = mapIter.next();
   // item: { value: [ 'storage-volumes', 'storage-volumes' ], done: false }
   console.log(item.value, item.done);
} while (!item.done);

console.log(myMap.size, myMap.get('san-managers'), myMap.get(undefined), myMap.get(NaN));
// 13 'fc-sans/device-managers' 'undefined value' 'not a number'

myMap.delete(undefined);
console.log(myMap.size, myMap.get(undefined));
// 12 undefined

myMap.forEach((value, key, map) => {
  console.log('forEach ---> myMap[', key, '] => ', value);
});
// forEach ---> myMap[ server-hardware ] =>  server-hardware
// forEach ---> myMap[ server-profiles ] =>  server-profiles
// forEach ---> myMap[ enclosures ] =>  enclosures
// forEach ---> myMap[ storage-pools ] =>  storage-pools
// forEach ---> myMap[ storage-systems ] =>  storage-systems
// forEach ---> myMap[ storage-volumes ] =>  storage-volumes
// forEach ---> myMap[ san-managers ] =>  fc-sans/device-managers
// forEach ---> myMap[ managed-sans ] =>  fc-sans/managed-sans
// forEach ---> myMap[ a string ] =>  value associated with 'a string'
// forEach ---> myMap[ {} ] =>  value associated with keyObj
// forEach ---> myMap[ function () {} ] =>  value associated with keyFunc
// forEach ---> myMap[ NaN ] =>  not a number

var KeyValuePairs = {
'server-hardware': 'server-hardware'
};

KeyValuePairs['server-profiles'] = 'server-profiles';
KeyValuePairs[undefined] = 'undefined';

console.log(KeyValuePairs[undefined]);
// undefined

console.log(KeyValuePairs['server-hardware']);
// 'server-hardware'

如果需要更多详细Map API More Details for Map on MDN

 

posted @ 2017-02-08 14:29  Hypocrite  阅读(334)  评论(0)    收藏  举报