Using arrays : Most elegant way to create a list of unique items in JavaScript on newest questions tagged arrays – Stack Overflow

In my CouchDB reduce function I need to reduce a list of items to the unique ones.

Note: In that case it’s ok to have a list, it will be a small number of items of string type.

My current way is to set keys of a object, then return the keys of that object
since the place the code can’t use things like _.uniq for example.

I’d like to find a more elegant way to spell it than this.

function(keys, values, rereduce) {
  // values is a Array of Arrays
  values = Array.concat.apply(null, values);
  var uniq = {};
  values.forEach(function(item) { uniq[item] = true; });
  return Object.keys(uniq);
}

See Answers


source: http://stackoverflow.com/questions/11688692/most-elegant-way-to-create-a-list-of-unique-items-in-javascript
Using arrays : using-arrays



online applications demo