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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:53:45+00:00 2026-06-18T09:53:45+00:00

I have two objects returned from two different rest resources. { id: 1, username:

  • 0

I have two objects returned from two different rest resources.

{
"id": 1,
"username": "jdoe"
}

{
"role_id": 1,
"role": "developer",
"members": [
    1,
    3,
    5
]
}

I’d like to use plain ole javascript to combine these two objects based into one where id is in the members array, so that i get back an object like has roles as the top node, then lists users of that role underneath.

Easily done?

  • 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-18T09:53:46+00:00Added an answer on June 18, 2026 at 9:53 am

    I had a similar problem a couple weeks ago with a set of 10 AJAX calls that I wanted to mesh together. A simple method is to create a lookup table from the id values, then use the lookup table to combine the results. Here’s an example using multiple users and multiple roles:

    var users = [
        { "id": 1, "username": "jdoe" },
        { "id": 3, "username": "dbob" },
        { "id": 5, "username": "jske" }
    ];
    var roles = [
        { "role_id": 1, "role": "developer", "members": [1,3,5] },
        { "role_id": 2, "role": "admin", "members": [5] }
    ];
    
    // create lookup table
    for (var i = 0; i < users.length; i++)
        users.lookup[users[i].id] = users[i];
    
    // populate members from users with lookup table
    for (var i = 0; i < roles.length; i++)
        for (var j = 0; j < roles[i].members.length; j++)
            roles[i].members[j] = users.lookup[roles[i].members[j]];
    

    Now the members in roles are references to the users rather than just the id’s:

    [
      { "role_id": 1, "role": "developer", "members":
        [{ "id": 1, "username": "jdoe" },
         { "id": 3, "username": "dbob" },
         { "id": 5, "username": "jske" }] },
      { "role_id": 2, "role": "admin", "members": 
        [{ "id": 5, "username": "jske" }] }
    ]
    

    And you can reference the usernames in a role like this:

    for (var i = 0; i < roles[0].members.length; i++)
        alert(roles[0].members[i].username);
    

    Demo: http://jsfiddle.net/2aqR5/1/

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

Sidebar

Related Questions

I have two objects List<Report> List<Newsletter> I need certain properties like Id Date Status
I have two complex rails (AR) queries coming from two different methods that I
If I have two queries SELECT Id, Forename, Surname FROM Person WHERE PersonName Like(‘%frank%’)
I have two tables that I would like to get data and from by
I have a process in my application that returns two different lists of objects.
I have two objects I need to create a relationship between in Core Data.
I have two objects and I want to merge them: public class Foo {
I have two objects that can be represented as an int, float, bool, or
I have two objects of the same class (Model) type. I want to check
Lets say we have two objects o1 & o2 defined as System.Object, in my

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.