Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:34:38+00:00 2026-06-08T13:34:38+00:00

I am trying to merge two sets of analytical data from two different providers.

  • 0

I am trying to merge two sets of analytical data from two different providers. The arrays, country name along with a metric, are arranged as follows:

[['Albania','1000'],['Australia','1000']]

Both data sets can contain different versions of the same country name (such as UK instead of United Kingdom). How can I go about merging these arrays of arrays in Javascript? By merge I mean combine the data sets for each country into a single set of arrays.

Example:

[['Albania','1000'],['United Kingdom','1000']]

+

[['Albania','1000'],['UK','1000']]

=

[['Albania','2000'],['United Kingdom','2000']]

Clarifications: Our mobile sites use one analytics provider where as our main global sites use another, different analytics provider. We need to merge these data sets to create an accurate report. There will only ever be 2 data sets needing to be merged.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:34:40+00:00Added an answer on June 8, 2026 at 1:34 pm

    Here is a partial solution translated from the PHP post. This does not get you aliasing. The equivalent code in PHP looks a lot cleaner because of associative arrays. You could obtain a very nice solution if you had object literals instead of arrays to work with. This solution may require shims for Array.indexOf and possibly Array.concat depending on your platform.

    http://jsfiddle.net/radu/8JQBS/

    var arr1 = [['Albania','1000'],['United Kingdom','1000']],
        arr2 = [['Albania','1000'],['UK','1000']],
        sum = [],
        index = -1;
    
    var mergedArray = arr1.concat(arr2);
    
    for (var i = 0, n = mergedArray.length; i !== n; i++) {
    
        for (var j = 0, m = sum.length; j !== m; j++) {   
            if (sum[j].indexOf(mergedArray[i][0]) !== -1) {
                index = j;
                break;            
            } else {
                index = -1;
            }
        }
    
        if (index !== -1) {
            sum[index][1] = (
                parseInt(sum[index][1], 10) +  
                parseInt(mergedArray[i][1], 10)
            ).toString();
        } else {
            sum.push([mergedArray[i][0], mergedArray[i][1]]);
        }
    }
    

    This produces: [['Albania', '2000'], ['United Kingdom', '1000'], ['UK', '1000']]. You could implement aliasing for this, but it’s annoying – see below for a better solution.

    Here is another solution which instead of producing an array of arrays as a result, gives an object literal. In my opinion this is much better. You may not be able to control the data you get from whatever API you’re using, but you can control how you process it, and this should make downstream code better. This may require a shim for Object.hasOwnProperty.

    http://jsfiddle.net/radu/fPsdc/

    var arr1 = [['Albania','1000'],['United Kingdom','1000']],
        arr2 = [['Albania','1000'],['UK','1000']], 
        mergedArray = arr1.concat(arr2),
        sum = {},
    
        // property is preferred name
        // define aliases in lowercase
        aliases = {
            'uk' : 'United Kingdom'
        };
    
    for (var i = 0, n = mergedArray.length; i !== n; i++) {
        var country = mergedArray[i][0],
            num = parseInt(mergedArray[i][1], 10);
    
        if (aliases.hasOwnProperty(country.toLowerCase())) {
            country = aliases[country.toLowerCase()];          
        }
    
        if (sum.hasOwnProperty(country))
            sum[country] += num;        
        else
            sum[country] = num;
    }
    

    This produces: {'Albania' : 2000, 'United Kingdom' : 2000}. If you really must have an array of arrays as a result.. you can do this:

    var arrSum = [];
    
    for (var prop in sum) {
        if (sum.hasOwnProperty(prop)) {
            arrSum.push([prop, sum[prop]]);
        }        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to merge two data.frames together, based on a common column name
I am trying to merge the data from two tables into one result but
Hi everyone I'm trying to merge two queries: SELECT DISTINCT name,$price FROM room JOIN
I'm trying to merge two arrays for the values of array1 that are set
im trying to merge two arrays together. both have numeric keys and are unique.
I'm trying to merge two file that have the same structure, and some data
I have two arrays which I'm trying to merge based on the date of
I'm trying to merge two arrays that have some overlapping results and some that
I'm having tremendous difficulty trying to merge two columns from two tables in PL/SQL.
Im trying to find simplest way to merge two arrays into the third one,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.