score:1

You could solve this problem using d3.set() method: https://github.com/d3/d3-collection/blob/master/README.md#sets.

The code will be:

var data=[{"code":5222,"date":3-4-2015},{"code":5222,"date":3-6-2015},{"code":5222,"date":3-7-2015},];

var Unique = d3.set(data, function(d) {return d.code});

Unique returns only '5222', as you expected.

Please, note that d3 converts data to strings in this case, so it might be the case you would need to convert them back to numbers.

score:3

I think you need to use nest().

var nest = d3.nest()
  .key(function(d) { return d.code; })
  .entries(data);

That will create a new array with each code only once and an object of all the objects that have that value.

Edit

var data=[{"code":5222,"date":3-4-2015},{"code":5222,"date":3-6-2015},{"code":5222,"date":3-7-2015}];

var nest = d3.nest()
  .key(function(d) { return d.code;})
  .entries(data);

d3.select("#detectors").selectAll("option")
  .data(nest)
  .enter()
    .append("option")
    .text(function(d){ return d.key;})
    .attr("value",function(d){return d.key;});

This code works for me with no errors


Related Query