How would you find the minimum value in this object in d3?
{Mon: 3.7, Tues: 1.2, Wed: 2.4, Thurs: 9.6....}
How would you find the minimum value in this object in d3?
{Mon: 3.7, Tues: 1.2, Wed: 2.4, Thurs: 9.6....}
If you just want the value itself, use d3.min and d3.values:
var obj = {Mon: 3.7, Tues: 1.2, Wed: 1.4, Thurs: 9.6};
var min = d3.min(d3.values(obj)); // 1.2
If you want to know the member that contains the min value, use d3.entries and Array.reduce:
min = d3.entries(obj).reduce(function(memo, item){
return memo.value > item.value ? memo : item;
}); // {key: "Tues", value: 1.2}
You could use underscorejs max:
var obj = {Mon: 3.7, Tues: 1.2, Wed: 2.4, Thurs: 9.6};
var max = _.max(obj); // 9.6
and userscorejs min:
var obj = {Mon: 3.7, Tues: 1.2, Wed: 2.4, Thurs: 9.6};
var min = _.min(obj ); // 1.2
You could make a loop that goes through the object and compares its values. Heres a code:
var obj= {Mon: 3.7, Tues: 1.2, Wed: 2.4, Thurs: 9.6};
var keys = Object.keys(obj);
var leastEl = obj[keys[0]];
for (var i = 0; i < keys.length; i++) {
if (leastEl > obj[keys[i]]) {
leastEl = obj[keys[i]];
}
}
edit: and yeah - as someone already stated above - your given example doesnt have any array inside, it has an object.
var obj = {Mon: 3.7,Tues: 1.2,Wed: 1.4}
var min;
for(var x in obj){
if(min == undefined || obj[x]<min){
min=obj[x];
}
}
console.log(min);