Lodash uniqBy多个数组?

时间:2016-05-23 14:43:10

标签: javascript underscore.js lodash

我有阵列:

[ [ '245', '0', '0.0100', '245,0,0.01' ],
  [ '245', '1', '0.0100', '245,1,0.01' ],
  [ '208', '0', '0.0300', '208,0,0.03' ],
  [ '208', '0', '0.0500', '208,0,0.05' ],
  [ '208', '0', '0.0600', '208,0,0.06' ] ]

我需要通过[0][1]单元获取唯一数组 像这样:

[ [ '245', '0', '0.0100', '245,0,0.01' ],
  [ '245', '1', '0.0100', '245,1,0.01' ],
  [ '208', '0', '0.0600', '208,0,0.06' ] ]

请帮助/

1 个答案:

答案 0 :(得分:6)

使用lodhash uniqBy() 方法

var data = [
  ['245', '0', '0.0100', '245,0,0.01'],
  ['245', '1', '0.0100', '245,1,0.01'],
  ['208', '0', '0.0300', '208,0,0.03'],
  ['208', '0', '0.0500', '208,0,0.05'],
  ['208', '0', '0.0600', '208,0,0.06']
];

console.log(
  _.uniqBy(data, function(v) {
    return v[0] + ' ' + v[1];
  })
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.0/lodash.js"></script>