Unable to get the value from object

I’m creating an app in React and trying to get data from object via function for rendering.
However, I’m getting errors and I banged my head on this for many hours but failed.
Help will be really appreciated.

I’m getting the following error:
TypeError: Cannot read property ‘title’ of undefined
I’m pasting the code below (Please see my comments inline):

render() {
    return (
        <div aria-live="assertive">
            <h2>Headlines</h2>
            <ul>
                {
                    Object.values(this.state.news).map((item) => {
                        console.log(item[0].title); //Doesn't work however:
                        console.log(item[0]); //This works and prints the object to the console properly.
                    })
                }
            </ul>
</div>

}

And here is the object structure:

{
"id": 36174,
"user_id": 1,
"feed_id": 6,
"status": "unread",
"hash": "e953071e47ce854a03508542f4f4eab471a386fd5fa200600486663c56240965",
"title": "SC junks plea seeking return of MPLAD funds donated to PM CARES Fund during COVID-19 pandemic",
"url": "https://www.firstpost.com/india/sc-junks-plea-seeking-return-of-mplad-funds-donated-to-pm-cares-fund-during-covid-19-pandemic-9311531.html",
"comments_url": "",
"published_at": "2021-02-16T20:07:56+05:30",
"created_at": "2021-02-16T18:29:32.804004+05:30",
"content": "<p>The Supreme Court Tuesday dismissed a plea seeking direction to the Centre to return Rs 365 crore donated by MPs from MPLADS fund to PM CARES Fund for tackling exigencies emerging from the COVID-19 pandemic.</p><p>A bench of Chief Justice Bobde and Justices AS Bopanna and V Ramasubramanian said it is not inclined to entertain the petition and dismissed it.</p><p>Advocate Dushyant Tiwari, appearing for petitioner Tushar Gupta, said that Rs 365 crore were donated by lawmakers from Members of Parliament Local Area Development Scheme (MPLADS) in the PM CARES Fund, in contravention of the guidelines.</p><p>Tiwari referred the RTI reply to buttress his claims and said that development work was hampered due to the diversion of the funds.</p><p>The plea filed by Gupta sought appropriate direction to the central government to return the entire donated amount in the bank account in which MPLADS funds are transferred every year.</p><p>The scheme&#39;s objective is to enable MPs to recommend developmental work &#34;with emphasis on the creation of durable community assets based on the locally felt needs to be taken up in their Constituencies, the plea said.</p><p>It said that right from inception of the scheme, durable assets of national priorities -- drinking water, primary education, public health, sanitation and roads -- are being created.</p><p>It is pertinent to note that in the month of March 2020, various MP&#39;s have donated MPLAD Fund for the pandemic and as per information a total number of 339 MP&#39;s have contributed a total of Rs 365 crore from their MPLADS to the Central pool for measures towards tackling exigencies in India emerging from the effect of the pandemic, the plea added.</p><p>The petition said that in April 2020 the MPLADS funds were suspended for two financial years -- 2020-2021 and 2021-2022.</p><p>Gupta referred to the RTI reply and said it raised few important questions and doubts related to the donation of the MPLAD Fund for the purpose of COVID-19.</p><p>The plea sought direction to the MPs to re-prioritise the earlier recommended works so that the incomplete work can be completed from the un-utilised amount available with the district authority.</p>",
"author": "Press Trust of India",
"share_code": "",
"starred": false,
"reading_time": 2,
"feed": {
    "id": 6,
    "user_id": 1,
    "feed_url": "https://www.firstpost.com/rss/india.xml",
    "site_url": "https://www.firstpost.com/category/india",
    "title": "Firstpost India News",
    "checked_at": "2021-02-16T18:29:32.331595+05:30",
    "next_check_at": "0001-01-01T00:00:00Z",
    "etag_header": "",
    "last_modified_header": "",
    "parsing_error_message": "",
    "parsing_error_count": 0,
    "scraper_rules": "",
    "rewrite_rules": "",
    "crawler": false,
    "blocklist_rules": "",
    "keeplist_rules": "",
    "user_agent": "",
    "username": "",
    "password": "",
    "disabled": false,
    "ignore_http_cache": false,
    "fetch_via_proxy": false,
    "category": {
        "id": 5,
        "title": "India News",
        "user_id": 1
    },
    "icon": {
        "feed_id": 6,
        "icon_id": 5
    }
}

}

So, here I’m unable to identify that if the complete object is being printed out in the console by using the second line (Please refer the code above) then why it is giving the error in first console.log call.
Please help

12 thoughts on “Unable to get the value from object”

  1. The corresponding map function is working with only with arrays. U are trying to output normal objects. Just Add

        <ul> {title} </ul>
        <ul> {feed.title} </ul>
    
    
    Reply

Leave a Comment