Javascript数组或对象

Javascript数组或对象,javascript,arrays,object,Javascript,Arrays,Object,我希望使用不同的API调用从不同的来源获取内容,并将它们整理成具有相同格式的对象或数组。我一直在使用javascript数组和对象,我发现的示例中似乎没有一个符合我的要求。这是我要存储的格式。这是我想要实现的一个示例的伪代码 var content = new Object(); getTweets(); getSoundCloud(); display(); function getTweets() { //first result content.type = "twe

我希望使用不同的API调用从不同的来源获取内容,并将它们整理成具有相同格式的对象或数组。我一直在使用javascript数组和对象,我发现的示例中似乎没有一个符合我的要求。这是我要存储的格式。这是我想要实现的一个示例的伪代码

var content = new Object();
getTweets();
getSoundCloud();
display();


function getTweets() {

    //first result
    content.type = "tweet
    content.text = "the text of the tweet"
    content.image = "the image from the tweet"

    return content;
}

function getSoundCloud() {
    //second result
    content.type = "soundcloud
    content.text = "the name of the song"
    content.image = "the image of the song"

    return content;
}


function display() {
    //for each content
    {
        $("#container").append(content.text);
    }

}
第一个结果由一个函数调用生成,第二个结果由不同的函数调用生成

我希望这些函数将所有内容一起添加到同一个format对象中。填充对象后,我希望在不同的函数中对其进行迭代,并在屏幕上显示

我如何制作这个物体?我试过了,但数据总是一样的,即用相同的值覆盖。在php中,这可能是一个关联数组或类似的东西

我想为我拥有的每一部分内容都有一个对象,并且可以循环使用它

content[0].type = "tweet"
content[1].type = "coundcloud"
任何有例子的建议都会很好

非常感谢

当你有很多事情需要立即思考时

我需要将这些东西存储在一个数组中

当你的“事物”复杂且具有各种属性/状态时,你应该立即思考:

我需要创建一个对象来存储它们

。。。所以这里最好的是一组对象。每个函数都将创建对象并将它们添加到
内容中,我们将切换到数组:

var content = []; // Array
getTweets();
getSoundCloud();
display();


function getTweets() {
    var tweet = {}; // This is an object

    tweet.type = "tweet";
    tweet.text = "The text of the tweet";
    tweet.image = "The image of the tweet";

    content.push(tweet); // add the object to the array.
}

function getSoundCloud() {
    var soundcloudThing = {};

    soundcloudThing.type = "soundcloud"
    soundcloudThing.text = "the name of the song"
    soundcloudThing.image = "the image of the song"

    content.push(soundcloudThing);
}
现在,当涉及到显示此内容时,就像使用数组一样;这里要做的最明显的事情就是迭代它

函数显示(){

对于(var i=0;i我在这里做了一个示例:

var content=[];
push(getTweets());
push(getSoundCloud());
显示();
函数getTweets(){
返回{
键入:“tweet”,
文本:“推特的文本”,
图片:“推特上的图片”
};
}
函数getSoundCloud(){
返回{
类型:“声音云”,
文字:“歌曲名称”,
图像:“歌曲的图像”
};
}
函数显示(){
content.forEach(功能(项){
$(“#容器”).append(item.text+”
); }); }​
内容
应该是一个对象数组,每个函数将其对象添加到内容数组中:

var content = []; // empty array
getTweets();
getSoundCloud();
display();


function getTweets() {
    content.push ({  //first result
      type: "tweet",
      text: "the text of the tweet",
      image: "the image from the tweet"
    });
}

function getSoundCloud() {
    content.push ({  //second result
      type: "soundcloud",
      text: "the name of the song",
      image: "the image of the song"
    });
}


function display() {
    content.forEach (v) {
       // code to format the object v appropriately
       var vtext = v.type + ' : ' + v.text;
        $("#container").append(vtext);
    }

}

事实上,我就是这么说的

var content = {
    text: this.text.linkify().linkuser().linktag(),
    image: this.profile_image_url,
    type: 'tweet'
};

arrayContent.push(content);
我现在可以循环浏览它,并显示每个内容

var content = []; // empty array
getTweets();
getSoundCloud();
display();


function getTweets() {
    content.push ({  //first result
      type: "tweet",
      text: "the text of the tweet",
      image: "the image from the tweet"
    });
}

function getSoundCloud() {
    content.push ({  //second result
      type: "soundcloud",
      text: "the name of the song",
      image: "the image of the song"
    });
}


function display() {
    content.forEach (v) {
       // code to format the object v appropriately
       var vtext = v.type + ' : ' + v.text;
        $("#container").append(vtext);
    }

}
var content = {
    text: this.text.linkify().linkuser().linktag(),
    image: this.profile_image_url,
    type: 'tweet'
};

arrayContent.push(content);