Skip to content

Instantly share code, notes, and snippets.

@ohcrfpv
Last active December 4, 2018 02:13
Show Gist options
  • Save ohcrfpv/b4fcc837d9c65a1f8f7c1b35406732b6 to your computer and use it in GitHub Desktop.
Save ohcrfpv/b4fcc837d9c65a1f8f7c1b35406732b6 to your computer and use it in GitHub Desktop.
underscorejs基本用法

字典转数组

var dct = {
  a: 1,
  b: 2,
  c: 3
};

var list = _.map(_.pairs(dct), (item)=> {
  return {
    key: item[0],
    value: item[1]
  }
});

// list =  [ {key: 'a', value: 1},
//           {key: 'b', value: 2},
//           {key: 'c', value: 3} ];

数组转字典

var list =  [ {key: 'a', value: 1},
              {key: 'b', value: 2},
              {key: 'c', value: 3} ];
var dct = {};
_.each(list, (item, index)=>{
  dct[item.key] = item.value;
});

// dct = {a: 1, b: 2, c: 3};              
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment