JavaScript JavaScript array sorting by summing up same elements javascript array sorting algorithm,javascript array sorting methods,javascript array

I was having some trouble when trying to sort and group the array items in JavaScript. Here is the sample inputs:

var arr = [
{merchantName: '', branchName: 'e', branchAddress: '', total: 10.5},
];

The output that I am trying to achieve:

var arr = [
{merchantName: '', branchName: '', branchAddress: '', total: 10.5},
];

I wanted to sort it in by branchName, for instance sum up the total for same branchName then at the same time bind all other attributes like merchantName and branchAddress together with it so that I can access them like:

for(var i = 0; i < arr.length; i++){
            console.log(arr[i].merchantName + ' ' + arr[i].branchName + ' ' + arr[i].branchAddress + ' ' + arr[i].total);
        }

I actually have no idea on how to even start it. Any ideas how to achieve it?

Thanks in advanced!

Answer:1

So here is how I would do it:

  1. Group the array into a hashmap based on the branchName property - calculate the total along with this.

  2. Take out the array from the hashmap and sort them

See demo below:

var arr = [
{merchantName: 'Giant', branchName: 'Giant Marine', branchAddress: 'Terrace 56 Branch Blk 56 Marine Terrace #01-259/261 Singapore 440056', total: 10.5},
{merchantName: 'Ntuc', branchName: 'Ntuc Zhongshan Mall Branch', branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', total: 12.149999999999999},
{merchantName: 'Giant', branchName: 'Giant Kim Keat 260 Branch', branchAddress: ' Blk 260 Kim Keat Avenue #01-01 Singapore 310260', total: 5.1},
{merchantName: 'Ntuc', branchName: 'Ntuc Scotts Square Branch', branchAddress: ' Scotts Square #B1-03 To 07 & #B1-10, 6 Scotts Road, 228209', total: 4},
{merchantName: 'Ntuc', branchName: 'Ntuc Zhongshan Mall Branch', branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', total: 4},
{merchantName: 'Ntuc', branchName: 'Ntuc Zhongshan Mall Branch', branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984', total: 8}
];

// create a hashmap
var hash = arr.reduce(function(p,c){
  if(!p[c.branchName])
    p[c.branchName] = c;
  else
    p[c.branchName].total += c.total;
  return p;
}, Object.create(null))

// now extract the result and sort them
var result = Object.keys(hash).map(function(e){
   return hash[e];
}).sort(function(a,b){
   return a.branchName - b.branchName;
});

console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}
Answer:2

A solution using reduce()

var arr = [{
    merchantName: 'Giant',
    branchName: 'Giant Marine',
    branchAddress: 'Terrace 56 Branch Blk 56 Marine Terrace #01-259/261 Singapore 440056',
    total: 10.5
  },
  {
    merchantName: 'Ntuc',
    branchName: 'Ntuc Zhongshan Mall Branch',
    branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984',
    total: 12.149999999999999
  },
  {
    merchantName: 'Giant',
    branchName: 'Giant Kim Keat 260 Branch',
    branchAddress: ' Blk 260 Kim Keat Avenue #01-01 Singapore 310260',
    total: 5.1
  },
  {
    merchantName: 'Ntuc',
    branchName: 'Ntuc Scotts Square Branch',
    branchAddress: ' Scotts Square #B1-03 To 07 & #B1-10, 6 Scotts Road, 228209',
    total: 4
  },
  {
    merchantName: 'Ntuc',
    branchName: 'Ntuc Zhongshan Mall Branch',
    branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984',
    total: 4
  },
  {
    merchantName: 'Ntuc',
    branchName: 'Ntuc Zhongshan Mall Branch',
    branchAddress: ' Zhongshan Mall Balestier #02-01, 20 Ah Hood Road, 329984',
    total: 8
  }
];

var newArr = arr.reduce(function(items, item) {

  var existing = items.find(function(i) {
    return i.branchName === item.branchName;
  });
  
  if (existing) {
    existing.total += item.total;
  } else {
    items.push(item);
  }
  
  return items;
}, []);

console.log(newArr);
Answer:3

We have multiple lists on the page that can have hundreds of items so for performance reasons, we'd like to use one-time bindings for things and only update when we know it needs to be updated and ...

We have multiple lists on the page that can have hundreds of items so for performance reasons, we'd like to use one-time bindings for things and only update when we know it needs to be updated and ...

Counting the items contained in an object, I used to have: Object.keys(state.posts).length Now, I have changed my posts state into an immutablejs Map, like so: Map({ "1": {title: "First Title"}, "2"...

Counting the items contained in an object, I used to have: Object.keys(state.posts).length Now, I have changed my posts state into an immutablejs Map, like so: Map({ "1": {title: "First Title"}, "2"...

I have the following radio buttons : <td valign="top"> <label>Have you ever been tested?</label> <input required id="Test" name="Test" type="radio" class="...

I have the following radio buttons : <td valign="top"> <label>Have you ever been tested?</label> <input required id="Test" name="Test" type="radio" class="...

  1. check radio button jquery
  2. check radio button javascript
  3. check radio button selected
  4. check radio button value
  5. check radio button html
  6. check radio button selected jquery
  7. check radio button js
  8. check radio button value jquery
  9. check radio button based on value jquery
  10. check radio button by default
  11. check radio button with keyboard
  12. check radio button selected c#
  13. check radio button is selected in selenium
  14. check radio button value in javascript
  15. check radio button on div click
  16. check radio button checked
  17. check radio button programmatically android
  18. check radio button jquery by id
  19. check radio button android
  20. check radio button jquery by name

I'm trying to open automatically a drop downdown menu when I hit the "Tab" key on the keyboard and focus my element.The focus function works, but the trigger doesn't. My intention is to force the "...

I'm trying to open automatically a drop downdown menu when I hit the "Tab" key on the keyboard and focus my element.The focus function works, but the trigger doesn't. My intention is to force the "...

  1. open dropdown select jquery
  2. open select dropdown on button click javascript
  3. open select dropdown on button click
  4. open select dropdown on button click jquery
  5. open select dropdown javascript
  6. open select dropdown on hover
  7. open select dropdown
  8. open select dropdown on click
  9. open select dropdown on enter
  10. open select dropdown using jquery
  11. open select dropdown programmatically
  12. open select dropdown on button click angular 6
  13. open select dropdown css
  14. openoffice drop down select
  15. open popup on dropdown select
  16. javascript open select dropdown programmatically
  17. js open select dropdown
  18. open modal on dropdown select
  19. html open select dropdown
  20. onclick open select dropdown