How to find the highest value in an array of objects based on multiple criteria in JavaScript

In this tutorial, we will be exploring how to search for the object with the highest value of a specific property in an array of objects, given a set of criteria. We will be using a code snippet that searches through an array of objects to find the object with the highest “promo_price” value that meets the criteria. We will break down each step of the code and explain what it does, so that you can understand the process and apply it to your own projects. By the end of this tutorial, you will have a better understanding of how to search for the highest value of a specific property in an array of objects.

  1. Define two arrays:
const firstArray = [11, 15, 16];

const secondArray = [
  {
    id: 71,
    promo_price: "10.00",
    promo_code : "GOLDBIKE23",
    auto_claim: 1,
    products: [
      {
        id: 11,
        title: "Sistem Sewaan Basikal - Gold",
      },
    ],
  },
  {
    id: 74,
    promo_price: "5.00",
    promo_code : "BIKERENT5",
    auto_claim: 1,
    products: [
     {
        id: 11,
        title: "Sistem Sewaan Basikal - Gold",
      },
      {
        id: 12,
        title: "Sistem Sewaan Basikal - Silver",
      },
    ],
  },
];

This step defines two arrays, “firstArray” and “secondArray”. The “firstArray” is a simple array of numbers, while the “secondArray” is an array of objects, where each object has several properties.

  1. Declare two variables to store the result:
let maxObj = null;
let maxPrice = 0;

This step declares two variables to store the result of the search. “maxObj” will be used to store the object with the highest “promo_price” value that meets the criteria, while “maxPrice” will be used to store the highest “promo_price” value found so far.

  1. Iterate through the “secondArray”:
secondArray.forEach((obj) => {
  // code block
});

This step uses the “forEach” method to iterate through each object in the “secondArray”. For each object, it executes the code block inside the parentheses.

  1. Check if the object meets the criteria:
if (obj.auto_claim === 1) {
  obj.products.forEach((product) => {
    if (firstArray.includes(product.id)) {
      // code block
    }
  });
}

This step checks if the current object meets the criteria. It checks if the “auto_claim” property of the object is equal to 1, and if the “products” array of the object contains an object whose “id” property matches an element in the “firstArray”. If the object meets these criteria, it executes the code block inside the parentheses.

  1. Compare the “promo_price” value:
const pricePromo = parseFloat(obj.promo_price);
if (pricePromo > maxPrice) {
  maxObj = obj;
  maxPrice = pricePromo ;
}

This step compares the “promo_price” value of the current object with the current maximum value stored in “maxPrice”. It first parses the “promo_price” value as a float using the “parseFloat” method, and stores it in a variable called “pricePromo”. If the “pricePromo” value is greater than the current “maxPrice” value, it updates the “maxObj” variable to the current object and updates the “maxPrice” variable to the new maximum value.

  1. Print the result to the console:
console.log(maxObj);

Here is the complete code sample:

 const firstArray = [11, 15, 16];
 const secondArray = [
    {
      id: 71,
      promo_price: "10.00",
      promo_code : "GOLDBIKE23",
      auto_claim: 1,
      products: [
        {
          id: 11,
          title: "Sistem Sewaan Basikal - Gold",
        },
      ],
    },
    {
      id: 74,
      promo_price: "5.00",
      promo_code : "BIKERENT5",
      auto_claim: 1,
      products: [
       {
          id: 11,
          title: "Sistem Sewaan Basikal - Gold",
        },
        {
          id: 12,
          title: "Sistem Sewaan Basikal - Silver",
        },
      ],
    },
  ];
  
  let maxObj = null;
  let maxPrice = 0;

  secondArray.forEach((obj) => {
    if (obj.auto_claim === 1) {
      obj.products.forEach((product) => {
        if (firstArray.includes(product.id)) {
          const promoPrice = parseFloat(obj.promo_price);
          if (promoPrice > maxPrice) {
            maxObj = obj;
            maxPrice = promoPrice;
          }
        }
      });
    }
  });
  
  console.log(maxObj);
  //   {
  //     "id": 71,
  //     "promo_price": "10.00",
  //     "promo_code": "GOLDBIKE23",
  //     "auto_claim": 1,
  //     "products": [
  //         {
  //             "id": 11,
  //             "title": "Sistem Sewaan Basikal - Gold"
  //         }
  //     ]
  // }

More JavaScript tutorial here.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *