0

数组如下所示:

arr[0] = "value";
arr[1] = "value2";

或者它可以是这样的:

var items = {
  "dioxanes"     :  0,
  "shunning"     :  1,
  "plowed"       :  2,
  "hoodlumism"   :  3,
  "cull"         :  4,
  "learnings"    :  5,
  "transmutes"   :  6,
  "cornels"      :  7,
  "undergrowths" :  8,
  "hobble"       :  9,
  "peplumed"     : 10,
  "fluffily"     : 11,
  "leadoff"      : 12,
  "dilemmas"     : 13,
  "firers"       : 14,
  "farmworks"    : 15,
  "anterior"     : 16,
  "flagpole"     : 17
};

现在这里的值将是百分比并且必须进行比较,但返回的值应该是最小值数组对象的名称。

如您所知,相同项目的值可以有 2 个或更多。

4

1 回答 1

0

items不是数组,它是一个对象。

var items = {"dioxanes": 0,  "shunning": 1,  "plowed": 2,
            "hoodlumism": 3, "cull": 4,      "learnings": 5,
            "transmutes": 6, "cornels": 7,   "undergrowths": 8,
            "hobble": 9,     "peplumed": 10, "fluffily": 11,
            "leadoff": 12,   "dilemmas": 13, "firers": 14,
            "farmworks": 15, "anterior": 16, "flagpole": 17};

var min = null, key_min, key;

for (key in items) {
  if (items.hasOwnProperty(key)) {
     if (min === null || items[key] < min) {
         min = items[key];
     key_min = key;
     }
  }
}

console.log("The key holds min value is: " + key_min);
于 2012-09-26T08:57:26.293 回答