开发者

How to modify multidimensional object to have named keys

开发者 https://www.devze.com 2023-03-24 05:32 出处:网络
How do I modify this code to return the structure below instea开发者_Python百科d of what the code currently generates..

How do I modify this code to return the structure below instea开发者_Python百科d of what the code currently generates..

current jQuery code and structure:

$(document).ready(function() {

var tweets = {};

 $.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?user_name=twitterapi&callback=?', function(data) {
        $.each(data, function(i) {
var arr = {};
arr['text'] =  this.text;
arr['id'] = this.id;
tweets[i] = [arr['text'] , arr['id']];
});

console.log(tweets);

returns

0 -> "tweet text(0), tweet id(0)"
 0 -> "tweet text(0)"
 1 -> "tweet id(0)"
1 -> "tweet text(1), tweet id(1)"
 0 -> "tweet text(1)"
 1 -> "tweet id(1)"
etc..

desired ..

tweets->
    tweet ->
     text -> "tweet text(0)"
     id -> "tweet id(0)"
    tweet ->
     text -> "tweet text(1)"
     id -> "tweet id(1)"
    etc..


Try this

var tweets = [];
    $.each(data, function(i) {

        tweets.push({ "tweet": { "text": this.text , "id": this.id } });;
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号