简体   繁体   中英

Cannot get minimum value from Array (Javascript)?

I'm trying to get the minimum value from an Array, and i come across this weird behavior. My expectation is to get 89 as the minimumDistance, instead i get 100. Can anyone explain this ?

// List 

var nodes = {
    "node": [
      {
        "name": "test",
        "id": "2",
        "edges": {
          "edge": [
            {
              "to": "4",
              "distance": "89"
            },
            {
              "to": "6",
              "distance": "100"
            },
            {
              "to": "8",
              "distance": "500"
            }
          ]
        }
      }]
}

// Initialization
startNode = 0
currentNode = startNode;


edge = nodes.node[currentNode].edges.edge;
var minimumDistance = 99999;

// Loop through the Neighbors of one Node
for (x= 0; x<edge.length; x++) {
  if (edge[x].distance < minimumDistance) {
      minimumDistance = edge[x].distance;
      } 
  document.write('Neighbor: ' + edge[x].to + ', Distance: ' + edge[x].distance);
  document.write('</br>');
}
 document.write('</br>');
document.write('Minimum Distance: ' + minimumDistance );

You need to take a number instead of a string for comparing. Comparing string is different from numbers. Here is some example:

 var nodes = { node: [{ name: "test", id: "2", edges: { edge: [{ to: "4", distance: "89" }, { to: "6", distance: "100" }, { to: "8", distance: "500" }] } }] }, startNode = 0, currentNode = startNode, edge = nodes.node[currentNode].edges.edge, minimumDistance = Infinity, // greatest value x; for (x = 0; x < edge.length; x++) { if (+edge[x].distance < minimumDistance) { // unary plus for getting a number minimumDistance = edge[x].distance; } document.write('Neighbor: ' + edge[x].to + ', Distance: ' + edge[x].distance); document.write('</br>'); } document.write('</br>'); document.write('Minimum Distance: ' + minimumDistance );

Let's break this down to a minimum code example. You are looking for the minimum value of the property distance within the edge array within your (imho overly nested) nodes object.

Problem is that the distance values are strings, not numbers. So, the values should be converted to Number to be able to compare them and determine the minimum of the distance values. Now the distances are mapped to Number by using +[a numeric string value] .

To determine the minimum value you can subsequently apply Math.min to the mapped array of numeric values.

 const edge = [ { "to": "4", "distance": "89" }, { "to": "6", "distance": "100" }, { "to": "8", "distance": "500" } ]; // map distances to numeric values const distancesFromEdge = edge.map( val => +val.distance ); // determine the minimum value of the mapped values const minDistance = Math.min.apply(null, distancesFromEdge); console.log(minDistance);

As Robin pointed out - you're comparing strings. If you can't store integers in your data source, you can do the following with parseInt() ;

if (parseInt(edge[x].distance) < minimumDistance) {
      minimumDistance = parseInt(edge[x].distance);
      } 

Change the strings to numbers in distance object.

var nodes = {
    "node": [
      {
        "name": "test",
        "id": "2",
        "edges": {
          "edge": [
            {
              "to": "4",
              "distance": 89
            },
            {
              "to": "6",
              "distance": 100
            },
            {
              "to": "8",
              "distance": 500
            }
          ]
        }
      }]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM