javascript - Building a custom JSON array from multiple arrays -
i'm getting list of user's friends both facebook's , 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 want build array this(nb: i'm appending provider key identify source of 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.
var facebook_friends = { "data": [{"name": "friend joe", "id": "123"}, {"name": "friend jane", "id": "12342"}]}; $.map(facebook_friends.data, function($item) { $item['provider'] = 'facebook'; homecoming $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'; homecoming $item; }); var all_friends = $.merge(facebook_friends.data, twitter_friends.users); console.log(all_friends); demo
javascript jquery ruby-on-rails arrays json
No comments:
Post a Comment