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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:17:06+00:00 2026-06-17T10:17:06+00:00

I have 2 arrays with the following data: Array1 = [A, A, A, A,

  • 0

I have 2 arrays with the following data:

Array1 = [A, A, A, A, B, B, B, C, C, C, C, C];    
Array2 = [4, 2, 4, 6, 3, 9, 6, 5, 4, 6, 2, 8];

I want to create 2 new arrays from these values:

Array3 = [A, B, C];    
Array4 = [4, 6, 5];

The values in Array 4 are the averages from Array2.

How should the javascript or jquery code look like to create Array3 and Array4?

edit:

I would like to group the values like this:

Array1      Array2
  A           2
  A           4
------------------
  B           2
  B           6
------------------
  C           3
  C           6
  C           9

     result:

Array3      Array4
  A           3          Average from 2 and 4
  B           4          Avarage from 2 and 6
  C           6          Average from 3, 6 and 9
  • 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-17T10:17:08+00:00Added an answer on June 17, 2026 at 10:17 am

    Try

    // Your initial Variables
    Array1 = ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'C', 'C', 'C', 'C', 'C'];    
    Array2 = [4, 2, 4, 6, 3, 9, 6, 5, 4, 6, 2, 8];
    
    // a temporary variable where we store our progress
    var groups = {}
    
    $.each(Array1, function(index,item){
        if (!groups[item]){
            groups[item] = {sum:0, count:0};
        }
        groups[item].sum += Array2[index]; // sum values belonging to same key 
        groups[item].count++; // increase counter of values per key
    });
    
    // create the letter only array
    var Array3 = $.map(groups,function(value,key){
        return key;
    });
    // create the averages array
    var Array4 = $.map(groups,function(value,key){
        return value.sum / value.count;
    });
    
    // show the results
    console.log(Array3, Array4);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following two arrays. I want the difference between these two arrays. That
I have the following string arrays: var array1 = new String[] { A, B,
I have the following JavaScript data structure. a[] is an array with text strings
Suppose I have two arrays like the following: $arr2 = array(array(first, second), array(third, fourth));
I have the following string arrays: var myArray1 = new string[] {A, B, C};
I have the following array setup, i,e: var myArray = new Array(); Using this
I am new PHP question and I am trying to create an array from
Currently, I have a tableView which is loaded from the following array called exerciseArray
I want to do following: Make Oracle stored procedure for read all data from
I have the following code function generate_pdf() { $fdf_data_strings = $this->get_hash_for_pdf(); #$fdf_data_names = array('49a'

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.