如何访问数组中对象的部分?

时间:2018-03-03 23:36:15

标签: javascript arrays object dom

我有这段代码。

我想只显示第一个帖子对象中的文本元素。我该怎么做?

我可以让控制台日志显示每个帖子对象,但我似乎无法将其缩小到只有第一个帖子对象,那么只有那个文本元素。

我的源代码:

 var data = {
        posts: [{
                text: 'working hard!',
                timestamp: 1519673597135,
                photo: 'https://simple.showdeo.com/f/cs/javascript/walkthroughs/assets/turtle-sandwich.png',
                username: 'profjason',
                likes: 43,
                comments: [{
                        text: "funny looking turtle",
                        username: 'tommy',
                        timestamp: 1519804800000,
                    },
                    {
                        text: "@profjason looks like you",
                        username: 'sarahlives',
                        timestamp: 1519856607000,
                    }
                ]
            },
            {
                text: 'where i will be #wheniretire',
                timestamp: 1518681617827,
                photo: 'https://simple.showdeo.com/f/cs/javascript/walkthroughs/assets/resort.png',
                username: 'tommy',
                likes: 186,
                comments: [{
                        text: "@tommy you will NEVER retire! too many spenders in your camp!",
                        username: 'tommy',
                        timestamp: 1518819807000,
                    },
                    {
                        text: "i will be your neighbor",
                        username: 'dropandroll',
                        timestamp: 1518835500000,
                    },
                    {
                        text: "already own that house and don't take company",
                        username: 'profjason',
                        timestamp: 1518977160000,
                    }
                ]
            },
            {
                text: 'did he live',
                timestamp: 1519027278654,
                photo: 'https://simple.showdeo.com/f/cs/javascript/walkthroughs/assets/shark-wave.png',
                username: 'sarahlives',
                likes: 22,
                comments: [{
                    text: "yep",
                    username: 'profjason',
                    timestamp: 1518977160000,
                }]
            },
            {
                text: 'wow...who knows that is out there. i wanted to be an astronaut, but decided to work fast food instead because i like to help people.',
                timestamp: 1513929600000,
                photo: 'https://simple.showdeo.com/f/cs/javascript/walkthroughs/assets/space.png',
                username: 'nocluebill',
                likes: 1,
            },
            {
                text: 'smash it like a bug!',
                timestamp: 1462258800000,
                photo: 'https://simple.showdeo.com/f/cs/javascript/walkthroughs/assets/parrot-smash.png',
                username: 'dropandroll',
                likes: 22,
                comments: [{
                        text: "this is how my dad taught me to do it too!",
                        username: 'tommy',
                        timestamp: 1498703760000,
                    },
                    {
                        text: "not happening",
                        username: 'nocluebill',
                        timestamp: 1498839900000,
                    },
                    {
                        text: "way too hard",
                        username: 'nocluebill',
                        timestamp: 1498875900000,
                    },
                    {
                        text: "birds can do anything",
                        username: 'sarahlives',
                        timestamp: 1499307900000,
                    },
                    {
                        text: "you should really take this picture down. it is not fair to the bird to put it on a skateboard",
                        username: 'profjason',
                        timestamp: 1505653800000,
                    }
                ]
            }
        ]
    }

我尝试了这个,但它只显示了所有帖子对象。

 console.log(data["posts"]);

非常感谢你的帮助!

0 个答案:

没有答案