loop over an object and return lowest number in javascript

纵饮孤独 提交于 2019-12-11 04:34:15

问题


So I'm a JS newbie and am trying to figure out how to fix my problem. I am trying to loop over an object and return the lowest number.

in my var shortest = ; if I hardcode a number say var shortest = 455; then the problem returns the correct number 3. although im not sure what to put there to make it blank by default. I have tried object[0], object[key] and '' and none of these work properly

var myObj = {first: 45, second: 23, third: 3, fourth: 222, fifth: 2343};

var myFunc = function (object) {
    var shortest = ;
    for (var key in object) {
      if (object[key] < shortest) {
        shortest = object[key];
      }
    }; 
    return shortest;
};

回答1:


set initial value of shortest to Number.MAX_VALUE is all you need

var myObj = {first: 45, second: 23, third: 3, fourth: 222, fifth: 2343};

var myFunc = function (object) {
    var shortest = Number.MAX_VALUE;
    for (var key in object) {
      if (object[key] < shortest) {
        shortest = object[key];
      }
    }; 
    return shortest;
};

Number.MAX_VALUE The largest positive representable number.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number




回答2:


Try this:

var myObj = {first: 45, second: 23, third: 3, fourth: 222, fifth: 2343};

    var myFunc = function (object) {
        var shortest = object.first;
        for (var key in object) {
          if (object[key] < shortest) {
            shortest = object[key];
          }
        }; 
        return shortest;
    };



回答3:


The reason it fails is in here:

var myFunc = function (object) {
    var shortest = ;
    for (var key in object) {
      if (object[key] < shortest) {

Since the value of shortest is undefined, the < operator causes it to be converted to NaN for the test, and according to the rules for The Abstract Relational Comparison Algorithm, comaring anything to NaN returns undefined, which is equivalent to false here.

The fix is to initialise shortest to some value, e.g. as qiu-deqing suggested Number.MAX_VALUE, or you can use something like:

function getShortest(object) {
    var shortest, value;

    for (var key in object) {
      value = object[key];

      if (typeof value == 'number' && !isNaN(value)) {

        if (typeof shortest != 'number') {
          shortest = value;
        } else {
          if (value < shortest) {
            shortest = value;
          }
        }
      }
    }
    return shortest;
}

The above will ensure that you only compare numbers and that shortest will only be a number or undefined. There should probably be a hasOwnProperty test too to avoid inherited properties.




回答4:


See if this helps:

var lowest = function(ns) {
  return Math.min.apply(0, ns);
};

var values = function(obj) {
  var result = [];
  for (var i in obj) {
    result.push(obj[i]);
  }
  return result;
};

var result = lowest(values(myObj));

console.log(result); //=> 3


来源:https://stackoverflow.com/questions/20736034/loop-over-an-object-and-return-lowest-number-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!