TheOldWarrior
TheOldWarrior

Reputation: 23

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 );

Upvotes: 2

Views: 88

Answers (4)

KooiInc
KooiInc

Reputation: 122888

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);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386520

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 );

Upvotes: 2

Aabhushan Gautam
Aabhushan Gautam

Reputation: 228

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
            }
          ]
        }
      }]
}

Upvotes: 0

Ludo
Ludo

Reputation: 157

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);
      } 

Upvotes: 0

Related Questions