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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:07:23+00:00 2026-06-17T12:07:23+00:00

I have array like this abcArr = [[A, 10], [B, 20], [A,30],[C,40]] how can

  • 0

I have array like this

abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]]

how can I group and sum values by A, B, C…

$.each(abcArr , function() {
    if (this[0] == this[0]) { 
      this[1] + this[1] 
    }; // I know this will simple double [1] values :(
});

desired result should be

[["A", 40], ["B", 20], ["C",30]]
  • 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-17T12:07:24+00:00Added an answer on June 17, 2026 at 12:07 pm

    Here’s a plain javascript way to do it that collects the unique indexes in a map and totals them as it does, then regenerates the array with the totals:

    abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]];
    
    var items = {}, base, key;
    for (var i = 0; i < abcArr.length; i++) {
        base = abcArr[i];
        key = base[0];
        // if not already present in the map, add a zeroed item in the map
        if (!items[key]) {
            items[key] = 0;
        }
        // add new item to the map entry
        items[key] += base[1];
    }
    
    // Now, generate new array
    var outputArr = [], temp;
    for (key in items) {
        // create array entry for the map value
        temp = [key, items[key]]
        // put array entry into the output array
        outputArr.push(temp);
    }
    
    // outputArr contains the result
    

    Working demo: http://jsfiddle.net/jfriend00/vPMwu/


    Here’s a way using jQuery’s .each:

    abcArr = [["A", 10], ["B", 20], ["A",30],["C",40]];
    
    var items = {}, base, key;
    $.each(abcArr, function(index, val) {
        key = val[0];
        if (!items[key]) {
            items[key] = 0;
        }
        items[key] += val[1];
    });
    
    var outputArr = [];
    $.each(items, function(key, val) {
        outputArr.push([key, val]);
    });
    
    // outputArr contains the result
    document.body.innerHTML = JSON.stringify(outputArr);
    

    Working demo: http://jsfiddle.net/jfriend00/Q8LLT/

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

Sidebar

Related Questions

I have an array like this that defines the entry values for a ListPreference:
I have an array like this with decimal values [$79.34, $99.23, $149, $139.34, $379,
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have an array like this var array = [{ order: 3, sub -
I have an array like this one $state[1]='FG'; $state[2]='BU'; $state[3]='CA'; ... $state[150]='YT' What I
i have an array like this: $arr = array(array('1', '0', 'nokia'), array('2', '0', 'samsung'),
I have an array like this: Array ( [2] => Array ( [0] =>
I have an array like this: object[] args and need to insert those args

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.