简体   繁体   中英

Field value showing null value

const book1 = this.state.books[0]; //giving one book
console.log(book1); //output->{id: 1, bookname: "Physics", price: 600, author: "ABC", pages: 567, …}
const {id,bookname,price,author,pages,category} = {book1};
console.log(price); //output->undefined

I have already tried a lot of things. How To get the value of particular property? Here is the JSON file:

 [
    {
            "id": 1,
            "bookname": "Physics",
            "price": 600,
            "author": "ABC",
            "pages": 567,
            "category" : "Science"
    }

]

The JavaScript object destructuring shown is invalid, because of the curly braces around book1 .

Remove those braces:

const { id, bookname, price, author, pages, category } = book1;

Here's a simpler example:

> const book = { price: 600 }
undefined
> const { price } = book
undefined
> price
600

Yes, as Jake mentioned, what you're trying to do here is called destructuring assignment. So as per the correct syntax,

const { id, bookname, price, author, pages, category } = book1;

this would actually mean,

const id=book1.id
const bookname=book1.bookname

And so on. You could have a look at https://javascript.info/destructuring-assignment for more information on destructuring assignment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM