skip to Main Content

Here is a sample of an array where i want to find the minimum from the .value in the array but when i try to do this which for loop it first give me the real minimum value but after it override with another value which not the minimum but the second last minimum
`

const AlzheimerData = [
    {
        "location": "world",
        "year": "2000",
        "value": "9.557819"
    },
    {
        "location": "world",
        "year": "2001",
        "value": "10.267346"
    },
    {
        "location": "world",
        "year": "2002",
        "value": "10.8148"
    },
    {
        "location": "world",
        "year": "2003",
        "value": "11.329527"
    }
]

var alzehemersmin = Number.MAX_VALUE;

for( data of AlzheimerData){
    if(data.value < alzehemersmin){
        alzehemersmin = data.value;
    }
    console.log(alzehemersmin);
}

output in console :

9.557819  --- Alzheimer.js:156:13 -- the real minimum value
10.267346 --- Alzheimer.js:156:13 -- the overided minimum value

I am expecting to get only the real minimum value which is the first one

3

Answers


  1. Using the Math function may be an alternative;

    const AlzheimerData = [
        {
            "location": "world",
            "year": "2000",
            "value": "9.557819"
        },
        {
            "location": "world",
            "year": "2001",
            "value": "10.267346"
        },
        {
            "location": "world",
            "year": "2002",
            "value": "10.8148"
        },
        {
            "location": "world",
            "year": "2003",
            "value": "11.329527"
        }
    ]
    
    function findMin(key) {
      const datas = AlzheimerData.map((node) => node[key]);
      return Math.min(...datas)
    }
    console.log(findMin('value'));
    Login or Signup to reply.
  2. It’s because you are comparing strings not floats. First, convert data.value to float and then compare them to find minimum.

    const AlzheimerData = [...]
    
    var alzehemersmin = Number.MAX_VALUE;
    
    for( data of AlzheimerData){
        const value = Number.parseFloat(data.value)
        alzehemersmin = Math.min(value,alzehemersmin)
    }
    
    console.log(alzehemersmin);
    
    Login or Signup to reply.
  3. This can be done in a one-liner:

    const alzheimerData = [
        {
            "location": "world",
            "year": "2000",
            "value": "9.557819"
        },
        {
            "location": "world",
            "year": "2001",
            "value": "10.267346"
        },
        {
            "location": "world",
            "year": "2002",
            "value": "10.8148"
        },
        {
            "location": "world",
            "year": "2003",
            "value": "11.329527"
        }
    ];
    
    const minValue = Math.min(...Object.values(alzheimerData).map(({value}) => parseFloat(value)));
    
    document.write(`The minimum value is: ${minValue}`);

    Read more about:

    1. Spread operator
    2. Destructing assignment
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search