Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reactjs i want to get the minumum price from this array list how can i get it

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading