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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:12:41+00:00 2026-06-13T05:12:41+00:00

I’m trying to take an array of many arrays that contain many objects and

  • 0

I’m trying to take an array of many arrays that contain many objects and manipulate it into an array of objects.

So, let’s say I have an array that looks like this:

[
    [
        {Name: 'Josh', email: 'josh@gmail.com', Points: 33},
        {Name: 'Doug', email: 'doug@gmail.com', Points: 12}
    ],
    [
        {Name: 'Josh', email: 'josh@gmail.com', Points: 11},
        {Name: 'Doug', email: 'doug@gmail.com', Points: 18}
    ],
    [
        {Name: 'Josh', email: 'josh@gmail.com', Points: 2},
        {Name: 'Doug', email: 'doug@gmail.com', Points: 27}
    ]
]​

The Desired outcome would be an array of objects that has a ‘Points’ property to hold an array of the points. So it would look like this:

[
    {Name: 'Josh', email: 'josh@gmail.com', Points: [33, 11, 2]},
    {Name: 'Doug', email: 'doug@gmail.com', Points: [12, 18, 27]}
]

This problem seems simple, but I can’t seem to figure out the best way of doing this. If you want this data in a fiddle, I made this for you to play with: http://jsfiddle.net/Qhxzz/1/

  • 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-13T05:12:43+00:00Added an answer on June 13, 2026 at 5:12 am

    Here’s one way using .reduce() and .forEach(). (You’ll need patches if you’re supporting older browsers.)

    var consolidated = data.reduce(function(holder, arr) {
        arr.forEach(function(obj) {
            if (obj.email in holder.emails)
                holder.emails[obj.email].Points.push(obj.Points);
            else {
                holder.emails[obj.email] = obj;
                holder.result.push(obj);
                obj.Points = [obj.Points];
            }
        });
        return holder;
    }, {emails:{},result:[]}).result;
    

    And actually, we could flatten out the original Array using concat.apply(), and be left with one monolithic Array to iterate.

    flat = [].concat.apply([], data);
    

    This turns your data into this structure:

    [{Name:"Josh", email:"josh@gmail.com", Points:33},
     {Name:"Doug", email:"doug@gmail.com", Points:12},
     {Name:"Josh", email:"josh@gmail.com", Points:11},
     {Name:"Doug", email:"doug@gmail.com", Points:18},
     {Name:"Josh", email:"josh@gmail.com", Points:2},
     {Name:"Doug", email:"doug@gmail.com", Points:27}]
    

    And makes the consolidation a bit simpler by eliminating the need for the inner .forEach().

    var consolidated = flat.reduce(function(holder, obj) {
        if (obj.email in holder.emails)
            holder.emails[obj.email].Points.push(obj.Points);
        else {
            holder.emails[obj.email] = obj;
            holder.result.push(obj);
            obj.Points = [obj.Points];
        }
        return holder;
    }, {emails:{}, result:[]}).result;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
Let's say I'm outputting a post title and in our database, it's Hello Y’all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I
I'm trying to create an if statement in PHP that prevents a single post

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.