4

I have an array of objects like below:

[
    {
        "id": 100,
        "Name": "T1",
        "amt": 15,
    },
    {
        "id": 102,
        "Name": "T3",
        "amt": 15,
    },
    {
        "id": 100,
        "Name": "T1",
        "amt": 20,
    },
    {
        "id": 105,
        "Name": "T6",
        "amt": 15,
    }
]

I want to filter the objects in the array by the minimum of amt. There are two objects with id's 100 but different amt (15 and 20). I want to filter the minimum value which is 15. The output should be:

[
    {
        "id": 100,
        "Name": "T1",
        "amt": 15,
    },
    {
        "id": 102,
        "Name": "T3",
        "amt": 15,
    },
    {
        "id": 105,
        "Name": "T6",
        "amt": 15,
    }
]

I followed this post but does not fit with my problem. Is there any simpler way of doing this, either pure JavaScript or lodash?

EugenSunic
  • 13,162
  • 13
  • 64
  • 86
Nguyen Hoang
  • 540
  • 5
  • 25
  • have you read [the docs for Array.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)? what have you tried so far? – Dan O Dec 27 '19 at 22:20
  • Yes, I already read the array filter but I have no idea how it work with my problem. Can you explain me a little bit? Thank you – Nguyen Hoang Dec 27 '19 at 22:23
  • Both entries with ID of 100 have amounts of 15 and 20 respectively, and they don't meet the minimum amount value of 100 as you said. Can you clarify your question again? – Terry Dec 27 '19 at 22:24

3 Answers3

3

You could group by id and take from every group the object with min value of amt.

var data = [{ id: 100, Name: "T1", amt: 15 }, { id: 102, Name: "T3", amt: 15 }, { id: 100, Name: "T1", amt: 20 }, { id: 105, Name: "T6", amt: 15 }],
    result = _(data)
        .groupBy('id')
        .map(group => _.minBy(group, 'amt'))
        .value();

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Use the standard algorithm for finding min value and apply the approach to the reduce function. When you find the min or the equal value to the min, add the current object to the array.

const arr = [{
    "id": 100,
    "Name": "T1",
    "amt": 15,
  },
  {
    "id": 102,
    "Name": "T3",
    "amt": 15,
  },
  {
    "id": 100,
    "Name": "T1",
    "amt": 20,
  },
  {
    "id": 105,
    "Name": "T6",
    "amt": 15,
  }
]
const minArr = arr.reduce((acc, curr) => curr.amt <= acc.min ? {
  ...acc,
  min: curr.amt,
  arr: [...acc.arr, curr]
} : acc, {
  min: Infinity,
  arr: []
}).arr
console.log(minArr);
EugenSunic
  • 13,162
  • 13
  • 64
  • 86
-3

You can do this using a for loop like so:

var minimum = 5;

for(var i = 0; i < yourArray; i++) {
    if(yourArray[i].amt < minimum) {
        console.log("The " + i + " item in the array's amount is less than the minimum: " + minimum);
    }
}

Or you can use Array.filter:

var minimum = 5;

function isBigEnough(value) {
  return value >= minimum
}

someArray.filter(isBigEnough)
Kenzoid
  • 276
  • 1
  • 16