javascript - How to combine json object by "foreign key" relationship - Stack Overflow

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

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 bine 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?

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 bine 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?

Share Improve this question edited Feb 1, 2013 at 13:49 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Feb 1, 2013 at 13:46 JasonJason 3492 silver badges16 bronze badges 2
  • 4 So this question about manipulation JavaScript objects, not JSON? It would be helpful if you could provide an example of the result. There is no such thing as a "JSON object": benalman./news/2010/03/theres-no-such-thing-as-a-json. – Felix Kling Commented Feb 1, 2013 at 13:48
  • To each their own. As the article you linked to states, Crockford use the term JSON Object. But that's a good point, +1 . – Jason Commented Feb 1, 2013 at 17:04
Add a ment  | 

1 Answer 1

Reset to default 5

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 bine 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/2aqR5/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208513a4616706.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信