2

I'm getting a list of a user's friends from both facebook's and twitter's API.

var facebook_friends = { "data": [{"name": "Friend Joe", "id": "123"}, {"name": "Friend Jane", "id": "12342"}]}
var twitter_friends = { "users": [{"name": "Other friend joe", "id": "333"}, {"name": "Other friend Jane", "id": "456"}]}

And i want to build an array like this(Nb: i'm appending the provider key to identify the source of the data)

var all_friends = [{"name": "Friend Joe", "id": "123", "provider": "facebook"},{"name": "Friend Jane", "id": "12342", "provider": "facebook"}, {"name": "Other friend joe", "id": "333", "provider": "twitter"},{"name": "Other friend Jane", "id": "456", "provider": "twitter"}]

Thanks.

1 Answer 1

4
var facebook_friends = { "data": [{"name": "Friend Joe", "id": "123"}, {"name": "Friend Jane", "id": "12342"}]};

$.map(facebook_friends.data, function($item) {
    $item['provider'] = 'facebook';
    return $item;    
});

var twitter_friends = { "users": [{"name": "Other friend joe", "id": "333"}, {"name": "Other friend Jane", "id": "456"}]};

$.map(twitter_friends.users, function($item) {
    $item['provider'] = 'twitter';
    return $item;    
});

var all_friends = $.merge(facebook_friends.data, twitter_friends.users);

console.log(all_friends);

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @martynas, do you know how to add the provider key in each array?
Like in the all_friends variable
aaaah, didn't notice that. give me a minute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.