Take the smallest value of an object array

Asked

Viewed 608 times

2

I have this array of objects, how can I make to return the object with the lowest value in currentTasks

"programmers":[
    {
        "id": 1,
        "name": "Eduardo Candido",
        "currentTasks": 1
    },
    {
        "id":2,
        "name": "João Lemos",
        "currentTasks": 1
    },
    {
        "id":3,
        "name": "Antonio Bond",
        "currentTasks": 2
    }
]
  • In this case it is the smallest objects? why you can have more than one minor, like the example, you want only the first two if that were the case, since they are 1.

  • Exactly, or any of the first two

  • https://answall.com/questions/285005/howto get o-maior-valor-em-um-array-com-javascript &#Xa.

2 answers

7


You can spread an array to Math.min to give you the lowest value.

For example:

const valores = object.programmers.map(({currentTasks}) => currentTasks)
const menorValor = Math.min(...valores);

And then you can use the .filter( to select the elements of this array that have the value you are looking for.

An example would be like this:

const object = {
  "programmers": [{
      "id": 1,
      "name": "Eduardo Candido",
      "currentTasks": 1
    },
    {
      "id": 2,
      "name": "João Lemos",
      "currentTasks": 1
    },
    {
      "id": 3,
      "name": "Antonio Bond",
      "currentTasks": 2
    }
  ]
};

const menorValor = Math.min(...object.programmers.map(({
  currentTasks
}) => currentTasks));
console.log('Menor valor', menorValor);
const menoresValores = object.programmers.filter(({
  currentTasks
}) => currentTasks === menorValor);
console.log('Menores', menoresValores);

4

With .reduce() you find the object with the lowest value in the key currentTasks:

const obj = { "programmers": [
    {
        "id": 1,
        "name": "Eduardo Candido",
        "currentTasks": 1
    },
    {
        "id":2,
        "name": "João Lemos",
        "currentTasks": 1
    },
    {
        "id":3,
        "name": "Antonio Bond",
        "currentTasks": 2
    },
    {
        "id":4,
        "name": "Fulano",
        "currentTasks": 3
    }
]}

const min = obj.programmers.reduce((a,b)=>{
   if(b.currentTasks < a.currentTasks) a = b;
   return a;
});

console.log(min);

In case there is more than one object with the key currentTasks with the same value, will return the first.

Another example with a value 0:

const obj = { "programmers": [
    {
        "id": 1,
        "name": "Eduardo Candido",
        "currentTasks": 1
    },
    {
        "id":2,
        "name": "João Lemos",
        "currentTasks": 1
    },
    {
        "id":3,
        "name": "Antonio Bond",
        "currentTasks": 0
    },
    {
        "id":4,
        "name": "Fulano",
        "currentTasks": 2
    }
]}

const min = obj.programmers.reduce((a,b)=>{
   if(b.currentTasks < a.currentTasks) a = b;
   return a;
});

console.log(min);

Reference:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.