0

So I have 2 arrays like these ...

var browser_names = ["Firefox", "Maxthon", "Opera", "Opera", "Chrome", "Chrome", "Edge", "Firefox"]

var user_count = [3, 3, 3, 3, 7, 20, 94, 142]

I want results like these

var result_browser_names = ["Firefox", "Maxthon", "Opera", "Chrome", "Edge"]

var result_user_count = [145, 3, 6, 27, 94]

As you can see 'result_browser_names' contains unique browser name values & 'result_user_count' contains 'sum of users' for each type of browser.

I have seen this solution, which works great for a single array. In my case I have 2 arrays ....

Any help is very much appreciated. Thanks

Community
  • 1
  • 1
Slyper
  • 896
  • 2
  • 15
  • 32
  • 1
    by seeing your example, I assume the 2 arrays should be consistent and align with each other all the time? If that's the case, why isn't the solution you link to not working, by doing them separately on both arrays? – shole Sep 05 '16 at 02:41
  • 1
    Do you have any specific problem with adapting the one-array solution to two arrays? – sth Sep 05 '16 at 02:42
  • Hi shole & sth. If I run the single array solution to my 'user_count' array. I will get incorrect results. – Slyper Sep 05 '16 at 02:53
  • Please post the code of what you tried. What happened? What did you expect to happen? What in particular is unclear? – Robert Sep 05 '16 at 03:46
  • Hi Robert, Seems like you haven't seen the answer below by Philip. Thanks – Slyper Sep 05 '16 at 05:39

2 Answers2

1

I'd suggest using an object. Assuming your 2 arrays will always match in length:

var browser_names = ["Firefox", "Maxthon", "Opera", "Opera", "Chrome", "Chrome", "Edge", "Firefox"]
var user_count = [3, 3, 3, 3, 7, 20, 94, 142]
var lib = {}

for (var i=0; i < browser_names.length; i++) {
  if (lib[browser_names[i]] != undefined) {
    lib[browser_names[i]] += user_count[i];
  } else {
    lib[browser_names[i]] = user_count[i];
  }
}

This should give you the browser names and the cumulative user counts for each browser saved within object lib

Also, for the if conditional in the loop, you could also do:

for (var i=0; i < browser_names.length; i++) {
  if (lib.hasOwnProperty(browser_names[i])) {
    lib[browser_names[i]] += user_count[i];
  } else {
    lib[browser_names[i]] = user_count[i];
  }
}

Also, I know your original question was an output to an array. You can easily loop through the keys of the object to get each of their respective browser names and user counts as well:

for (var k in lib) {
  console.log(k);         // Browser Names
  console.log(lib[k]);    // Their respective user counts
}
philip yoo
  • 2,462
  • 5
  • 22
  • 37
0

You could use a single loop and the help from an object as reference to the result arrays.

var browser_names = ["Firefox", "Maxthon", "Opera", "Opera", "Chrome", "Chrome", "Edge", "Firefox"],
    user_count = [3, 3, 3, 3, 7, 20, 94, 142],
    result_browser_names = [],
    result_user_count = [];

browser_names.forEach(function (b, i) {
    if (!(b in this)) {
        this[b] = result_browser_names.push(b) - 1;
        result_user_count.push(0);
    }
    result_user_count[this[b]] += user_count[i];
}, Object.create(null));

console.log(result_browser_names);
console.log(result_user_count);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392