0

I have this code.

I want to display only the text elements in the first posts object. How would I do this?

I can get the console log to display every posts object, but I cannot seem to narrow it down to only the first posts object, then only the text element inside that.

My source code:

 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,
                    }
                ]
            }
        ]
    }

I tried this, but it just displays all the posts objects.

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

Thanks a lot for your help!

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44
Brian Duncan
  • 115
  • 3
  • 12
  • 4
    `console.log(data.posts[0].text);` – Ele Mar 03 '18 at 23:37
  • [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Jonathan Lonowski Mar 03 '18 at 23:40
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – str Mar 04 '18 at 00:08

0 Answers0