I'm getting a list of a user's friends from both facebook's and twitter's API.
我从facebook和twitter的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)
我想构建一个这样的数组(Nb:我添加了provider键来标识数据的来源)
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 个解决方案
#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
#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);