title: Find the best matching combination of numbers
body: <p>I need to manipulate <code>chunks</code> <code>quantity</code> until total will be as close as possible to the <code>requirements</code>. Decimals are ok in the quantity.</p>
  <p>Again: I can change only <code>quantity</code> property.</p>
  <p>How do I do that efficiently?</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const chunks = { // &lt;- the chunks
    chunk1: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 19, b: 17, c: 10 } 
    },
    chunk2: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 17, b: 11, c: 15 } 
    },
    chunk3: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 7, b: 19, c: 0 } 
    },
    chunk4: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 14, b: 4, c: 19 } 
    },
    chunk5: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 3, b: 15, c: 6 } 
    },
    chunk6: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 10, b: 16, c: 3 } 
    },
    chunk7: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 2, b: 3, c: 2 } 
    },
    chunk8: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 14, b: 16, c: 11 } 
    },
    chunk9: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 7, b: 2, c: 2 } 
    },
    chunk10: {
      quantity: 0, // &lt;- the quantity
      payload: { a: 1, b: 7, c: 17 }
    }
  }
  
  const requirements = { // &lt;- the requirements
    a: 500,
    b: 1200,
    c: 1500
  }
  
  const total = {
    a: 0,
    b: 0,
    c: 0
  }
  
  // sum all chunks
  Object.keys(chunks).forEach(chunkId =&gt; {
    Object.keys(total).forEach(propId =&gt; {
      total[propId] += chunks[chunkId].payload[propId] * chunks[chunkId].quantity
    })
  })
  
  console.log(total) // &lt;- i need this to be as close as possible to the `requirements`.
  
  // MY SOLUTION:
  const percentages = JSON.parse(JSON.stringify(chunks))
  Object.keys(percentages).forEach(chunkId =&gt; {
    let total = 0
    Object.keys(percentages[chunkId].payload).forEach(propId =&gt; {
      const perc =
        percentages[chunkId].payload[propId] / (requirements[propId] / 100)
      percentages[chunkId].payload[propId] = perc
      total += perc
    })
    Object.keys(percentages[chunkId].payload).forEach(propId =&gt; {
      percentages[chunkId].payload[propId] =
        percentages[chunkId].payload[propId] / (total / 100)
    })
  })
  
  const myTotal = {
    a: 0,
    b: 0,
    c: 0
  }
  
  Array.from(Array(10)).forEach(() =&gt; {
    Object.keys(percentages).forEach(chunkId =&gt; {
      let highestPropId
      let highestPropPercentage = 0
      Object.keys(percentages[chunkId].payload).forEach(propId =&gt; {
        const perc = percentages[chunkId].payload[propId]
        if (perc &gt; highestPropPercentage) {
          highestPropPercentage = perc
          highestPropId = propId
        }
      })
      const remainingNum = requirements[highestPropId] - myTotal[highestPropId]
      const koe = 0.5
      const multiplier =
        (remainingNum / chunks[chunkId].payload[highestPropId]) * koe
      Object.keys(myTotal).forEach(propId =&gt; {
        myTotal[propId] += chunks[chunkId].payload[propId] * multiplier
      })
      chunks[chunkId].quantity += multiplier
    })
  })
  
  console.log('myTotal', myTotal)
  /*
  in the console log output you'll see this:
  {
    "a": 499.98450790851257,
    "b": 1202.1742982865637,
    "c": 1499.5877967505367
  }
  compare it with the `requirements` object above:
  const requirements = { // &lt;- the requirements
    a: 500,
    b: 1200,
    c: 1500
  }
  as you see, it's almost the same. I need more efficient solution
  */</code></pre>
  </div>
  </div>
  </p>
  <p>It's not accurate and quite inefficient. Also, it produces negative quantities. Any better options?</p>