Best combination of a sum between two parameters in an array of objects I am trying to build a simple algorithm to achieve the best combination of values based on two params in an array of objects.
The whole code is available here
First I have an array holding objects. The items which will be analyzed and packed into volumes together. All of the values are in percentages.
const nonStackableItems = [
    {
      id: 0,
      m2: 15,
      m3: 11,
      weight: 20
    },
    {
      id: 1,
      m2: 25,
      m3: 12,
      weight: 42
    },
    {
      id: 2,
      m2: 50,
      m3: 13,
      weight: 40
    },
    {
      id: 3,
      m2: 65,
      m3: 14,
      weight: 25
    }
  ];

The idea is to combine these objects into packages, respecting the maximum of 100% for each value (except for m3 in this case, as all the packages should have enough height in this first analysis).
The approach that I gave is just not good enough, as I am sorting this array by m2 then by weight, call the function and then compare the length of both, and then choose the result that creates less volumes.
This is the piece of code that does the calculations:
function nSVolHelper(arr, vol, param1, param2) {
    let newVol = {
      id: vol.length,
      m2: 0,
      m3: 0,
      weight: 0
    };
    for (let i = 0; i &lt; arr.length; i++) {
      const validation =
        newVol[param1] + arr[i][param1] &lt; 100 &amp;&amp;
        newVol[param2] + arr[i][param2] &lt; 100;
      if (validation) {
        newVol.m2 += arr[i].m2;
        newVol.m3 += arr[i].m3;
        newVol.weight += arr[i].weight;
      }
      if (!validation) {
        return nSVolHelper(arr.slice(i), [...vol, newVol], param1, param2);
      }
    }
    vol.push(newVol);
    return vol;
  }

  function nSVol(arr, param1, param2) {
    return nSVolHelper(arr, [], param1, param2);
  }

I would like to find a way of doing this without taking as a base the order of the array, to get the finest calculation without running unnecessary code, and I get the feeling that this approach of mine is just not reliable as well.
As an example, for better understanding:
If I pass the array sorted by weight I get a create 2 volumes.
console.log(
    &quot;call&quot;,
    nSVol(
      nonStackableItems.sort((a, b) =&gt; a.weight - b.weight),
      &quot;weight&quot;,
      &quot;m2&quot;
    )

If I pass this same array sorted by m2 I get 3 volumes.
console.log(
    &quot;call&quot;,
    nSVol(
      nonStackableItems.sort((a, b) =&gt; a.m2 - b.m2),
      &quot;weight&quot;,
      &quot;m2&quot;
    )

This was the best approach that I could find, but I feel that there could be another way of thinking of this.
You're right that your solution wouldn't be the most accurate. If I'm understanding your question right, what you just described is the Multi-dimensional 0-1 knapsack problem (see the &quot;Multi-dimensional knapsack problem&quot; heading in this wikipedia page). Solving these kinds of problems is not easy for computers to do quickly, and the best algorithm to use can largely depend on the type of data you're expecting to receive.
Are there very, very few items you're ever going to work with? How important is speed? Can you get away with a simple brute-force method, and try every possibility?
How much does accuracy matter? Maybe the particular use case doesn't necessarily need the best answer, just a good enough answer. If so, the code you wrote would work just fine.
Otherwise, I would read up a bit on the knapsack problem, and try googling &quot;Multi-dimensional 0-1 knapsack problem&quot; (multidimensional because you are working with multiple limits, not just weight. 0-1 because you either use the item or you don't, you can't use 1/2 an item in a package). My quick searching showed that there's a number of technical articles out there that address this exact issue. Knowing the name of the algorithm you need should help with finding more resources on it :).