0

I would like to combine the quantity values of objects if ohmage, tolerance, and wattage values are the same as previous checked objects. Starting object:

var x = [{
    date: "2020",
    ohmage: "1k45",
    quantity: 5000,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "9k34",
    quantity: 1000,
    tolerance: 2,
    wattage: 0.125
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3000,
    tolerance: 2,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3500,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 500,
    tolerance: 5,
    wattage: 0.5
}];

Desired object:

var x = [{
    date: "2020",
    ohmage: "1k45",
    quantity: 8500,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "9k34",
    quantity: 1000,
    tolerance: 2,
    wattage: 0.125
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3000,
    tolerance: 2,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 500,
    tolerance: 5,
    wattage: 0.5
}];

I took a look at Merge duplicate objects in array of objects and redid the function but it doesn't combine all objects which should be combined. My current function:

var seen = {};
array = array.filter(entry => {
    var previous;
    // Have we seen this ohmage before?
    if (seen.hasOwnProperty(entry.ohmage)) {
        if (entry.tolerance == seen[entry.ohmage].tolerance && entry.wattage == seen[entry.ohmage].wattage) {
            console.log(true)
            // Yes, grab it and add this quantity to it
            previous = seen[entry.ohmage];
            previous.quantity.push(entry.quantity);

            // Don't keep this entry, we've merged it into the previous one
            return false;
        }
    }
    // entry.quantity probably isn't an array; make it one for consistency
    if (!Array.isArray(entry.quantity)) {
        entry.quantity = [entry.quantity];
    }
    // Remember that we've seen it
    seen[entry.ohmage] = entry;

    // Keep this one, we'll merge any others that match into it
    return true;
});

1 Answer 1

1

array.filter function cannot modify array elements it's just left those one for which callback function returns true. To merge elements you can use reduce function:

const result = array.reduce((acc, entry) =>{
if (acc.filter(item => item.ohmage===entry.ohmage && item.tolerance===entry.tolerance && item.wattage===entry.wattage).length > 0)
{  
  // If an entry with parameters already exists
  let idx = acc.findIndex(item => item.ohmage===entry.ohmage && item.tolerance===entry.tolerance && item.wattage===entry.wattage)
  //Merge items by sum quantities as in example
  acc[idx] = {...acc[idx], quantity: acc[idx].quantity + entry.quantity}
  return acc
}

  // Otherwise adds entry to accumulator as it is
 return [...acc, entry]

}, [])

console.log(result)
<script>
var array = [{
    date: "2020",
    ohmage: "1k45",
    quantity: 5000,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "9k34",
    quantity: 1000,
    tolerance: 2,
    wattage: 0.125
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3000,
    tolerance: 2,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 3500,
    tolerance: 5,
    wattage: 2
}, {
    date: "2020",
    ohmage: "1k45",
    quantity: 500,
    tolerance: 5,
    wattage: 0.5
}];

</script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.