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 7581091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:06:50+00:00 2026-05-30T18:06:50+00:00

I need to group element of a multidimensional array in javascript. This is my

  • 0

I need to group element of a multidimensional array in javascript.
This is my array:

        var data = []; 
        data.push(["ASIA", "CHINA", 1992, 31689222674, 5]); 
        data.push(["ASIA", "CHINA", 1997, 31327384072, 5]); 
        data.push(["ASIA", "INDIA", 1992, 27910388563, 5]); 
        data.push(["ASIA", "INDIA", 1993, 28669881069, 5]); 
        data.push(["ASIA", "INDIA", 1997, 29421629324, 5]); 
        data.push(["ASIA", "INDONESIA", 1992, 30990831267, 5]); 
        data.push(["ASIA", "INDONESIA", 1997, 31478506122, 5]); 
        data.push(["ASIA", "JAPAN", 1992, 29239959577, 5]); 
        data.push(["ASIA", "JAPAN", 1996, 29901316952, 5]); 
        data.push(["ASIA", "JAPAN", 1997, 29860665098, 5]); 
        data.push(["ASIA", "VIETNAM", 1992, 27229816811, 5]); 
        data.push(["ASIA", "VIETNAM", 1996, 27553006375, 5]); 
        data.push(["ASIA", "VIETNAM", 1997, 27240345298, 5]); 

        data.push(["AMERICA", "BRAZIL", 1992, 31689222674, 5]); 
        data.push(["AMERICA", "BRAZIL", 1993, 31207214888, 5]); 
        data.push(["AMERICA", "BRAZIL", 1994, 31926517793, 5]); 
        data.push(["AMERICA", "EUA", 1992, 27910388563, 5]); 
        data.push(["AMERICA", "EUA", 1993, 28669881069, 5]); 
        data.push(["AMERICA", "EUA", 1994, 29343326950, 5]); 

I need to group elements by combining the contents of that array. I found this code, but it considers an array index.

function group(data, index) {
    var o;
    var other = {};
    $.each(data, function(i, value) {
        o = data[i][index];         
        if (!(o in other))
            other[o] = [];
        other[o].push(data[i]);
    })
    return other;           
}

Then the group with more indices should be as an example: grouping by index 1 + 2.

    ASIA - CHINA => ["ASIA", "CHINA", 1992, 31689222674, 5], 
                    ["ASIA", "CHINA", 1997, 31327384072, 5];
    ASIA - INDIA => ....
    ASIA - INDONESIA => .....
    ASIA - JAPAN => ....
    ASIA - VIETNAM =>

    AMERICA - BRAZIL => ....
    AMERICA - EUA => ....

This should also work for group involving more indices as well.

  • 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-05-30T18:06:51+00:00Added an answer on May 30, 2026 at 6:06 pm

    If you need this kind of functionality, underscore.js is highly recommended.

    var groupedData = _.groupBy(data, function(innerArray) {
        return innerArray[0] + "-" + innerArray[1];
    });
    

    You could also customize the function

    function groupByTwoIndexes(data, index1, index2) {
        var o;
        var other = {};
        $.each(data, function(i, value) {
            o = data[i][index1] + "-" + data[i][index2];         
            if (!(o in other))
                other[o] = [];
            other[o].push(data[i]);
        })
        return other;           
    }
    
    var groupedDataByTwo = groupByTwoIndexes(data, 0, 1);
    

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a group of strings in Javascript and I need to write a
Ok, I need keys to be preserved within this array and I just want
Given the XML below, I need to group by InputItem and sum the costs
Anyone know what group I need to belong to show up in the sp_help_operator
I need to update a group of cells by inserting the same two characters
I need to grant rights to Windows user group Everyone to the HKCR hive
I need to write a query that will group a large number of records
I need add a new user group for mediawiki. The new group has more
I have a need to determine what security group(s) a user is a member
I have got a group of radio buttons that when clicked need to display

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.