1

if i have a an array like below in JS

lineitems : [{
    quantity : 1,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 48.00,
    unitPriceLessTax: 40.00,
    SKU: 'SKU78910',
    productName: 'Red Shoes'
 }]

how do i conver it to look like below

lineitems : [{
    quantity : 2,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 48.00,
    unitPriceLessTax: 40.00,
    SKU: 'SKU78910',
    productName: 'Red Shoes'
 }]

basically looking to merge duplicates based on SKU

2
  • please form your code for better visibility
    – Saar
    Commented Sep 17, 2015 at 13:02
  • on what you want to compare? Commented Sep 17, 2015 at 13:12

4 Answers 4

1

you can use associative array:

var newLineItems = new Array();
$.each(lineItems, function (index) {
    if (newLineItems[this.SKU])
        newLineItems[this.SKU].quantity += this.quantity;
    else
        newLineItems[this.SKU] = this;
});
1
  • Use an object, not an array.
    – Andy
    Commented Sep 17, 2015 at 13:28
1

You can use Array.prototype.forEach()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

var result = []
var temp = [];
lineitems.forEach(function (element, index, array) {
    if (temp[element.SKU] === undefined)
        temp[element.SKU] = element;
    else
        temp[element.SKU].quantity += element.quantity;
});
for (var items in temp){
    result.push(temp[items]);
}

Hope it's useful

Dan

0

Pure JS; Fast calculator;

<script>
    var lineItems = [{
    quantity : 1,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 48.00,
    unitPriceLessTax: 40.00,
    SKU: 'SKU78910',
    productName: 'Red Shoes'
 }];

var nl =[], i=0;
var collapse = function ()
{
    if (lineItems.length<=i) return;
    if (nl[lineItems[i].SKU])
    {
        nl[lineItems[i].SKU].quantity+=lineItems[i].quantity;
    }
    else  nl[lineItems[i].SKU]=lineItems[i];
    i++;
    //lineItems.splice(0,1);
    collapse();
};
collapse();
console.log(nl);
var newLineItems = Object.keys(nl).map(function (key) {return nl[key]});
console.log(newLineItems);
console.log('new line items');
console.log(lineItems);
</script>

3
  • hi Leo, could you help me with the issue i am facing? basically when i runt the code the original Array gets overritten with value so i am assumin ghtis is because we are using "return nl[key]" how can i over come the problem where original values of "lineItems" is not touched but only "nl"
    – Ethan Cox
    Commented Sep 30, 2015 at 13:22
  • i have tried assigning the lineItems to another array and used the same but it still updates the original value. i have to validate the orginal array against the new one. thanks for you help
    – Ethan Cox
    Commented Sep 30, 2015 at 13:24
  • Hi Ethan, the reason it changes the original value was due to the splice. Now I've modified the code not to splice so it should not change the original values and should also gain some performance as we're not resizing the array any longer;
    – Leo Nix
    Commented Sep 30, 2015 at 13:40
0

Using lodash is a quick way to manage your collections:

result = _.uniq(lineitems, "SKU");
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.