I'm developing a Javascript and d3.js application where I'd like to keep track of unique values in a an uploaded dataset. Using one of the new Javascript Set objects seems appealing, but I can't figure out a good way to extract the max and min values of the set. My code:
var mySet = new Set();
mySet.add(5);
mySet.add(10);
mySet.add(20);
console.log( d3.max(mySet) );
This yields undefined rather than 20.
I've tried the .elements(), .values() and .keys() Set functions, which pass a SetIterator object to d3.max() but also return undefined. Using Array.from appears to be currently unsupported in Chrome.
Any suggestions? I realize that this may be browser-specific as Set objects get rolled out; I'm developing on Chrome v44. I've also looked at using an object object as a dictionary, but can't figure out a concise way to get max/min to work with that structure either.
Edit: I initially asked this question using a mix of arrays and numbers as set elements. The issue still appears in Chrome when using just elements, as above
You can find Maximum and Minimum values in one line using apply
const mySet = new Set([1, 2, 9, 7, 8]);
Math.max.apply(this, [...mySet]);
Math.min.apply(this, [...mySet]);
d3.max only takes an array (docs). Right now, you are passing in a set to d3.max that looks like:
{ [5,10,20], 21 }
When you should pass an array like this
[5,10,20,21]
Here's how you should do it:
var mySet = new Set();
mySet.add(5);
mySet.add(10);
mySet.add(20);
mySet.add(21);
myArray = []
// convert set to array
for (v of mySet) {
myArray.push(v)
}
console.log( d3.max(myArray) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With