我想从导入的文件中获取JSON数组的长度,但是它返回未定义的值。我可以从中访问任何元素。我想问题是这里的“书”不是数组吗?
book.json:
[
{
"author" : "Bill",
"total_chapters" : 14,
},
{
"author" : "Chan",
"total_chapters" : 29,
},
:
]
App.js:
import * as books from './assets/book.json';
const author = books[60].author; //returns author
console.log('#books: ' + books.length); //undefined
console.log('books is ' + books.constructor); // Books is function Object() { [native code] }
console.log('books: ' + books); //books: [object Object]
答案 0 :(得分:-2)
感谢提示形式@snjmhj。书籍是一个对象,可以通过以下方式获得长度:
Object.values(books).length