in reactjs im trying to this suppose my array list is something like this i want to get the minmum price from this list…suppose the ‘totalll’: 100 how can i do that please let me know. array->object->array->totalll
const data = [
{
name: "test",
lab_partners: [
{
id: 0,
totalll: 5000,
},
{
id: 1,
totalll: 200,
},
{
id: 2,
totalll: 1000,
},
],
},
{
name: "test",
lab_partners: [
{
id: 0,
totalll: 7000,
},
{
id: 1,
totalll: 100,
},
{
id: 2,
totalll: 800,
},
],
},
];
const minTotalByName = (data, id) => {
const totals = data.filter((x) => x.lab_partners.find((y) => y.id === id));
return Math.min(...totals);
};
const min = minTotalByName(data);
console.log(min, "min");
>Solution :
const totals = data
.map((d) => d.lab_partners)
.flat()
.map((p) => p.totalll);
const minTotal = Math.min(...totals);
and you’ll end up with 100 in minTotal.
If you need the full object with the lowest totalll, things get a little more complicated, but we can (for once with good conscience) use .reduce:
const lowestPartner = data
.map((d) => d.lab_partners)
.flat()
.reduce(
(currentMin, obj) =>
!currentMin || obj.totalll < currentMin.totalll ? obj : currentMin,
null,
);
// {id: 1, totalll: 100}