我正在尝试创建一个JavaScript对象,该对象可以轻松地让我检查一本书中有多少章,以及某章中有多少经文。
我的计划是使对象具有这样的结构:
const amounts = {
book: {
bookNumber: 1,
bookChapters: 50,
chapter:
{
chapterNumber: 1,
versesInChapter: 31
},
chapter:
{
chapterNumber: 2,
versesInChapter: 25
}
},
book: {
bookNumber: 2,
bookChapters: 40,
chapter:
{
chapterNumber: 1,
versesInChapter: 26
},
chapter:
{
chapterNumber: 2,
versesInChapter: 22
}
}
}
如何基于变量值访问此类结构的嵌套值?例如,我可能有一个var bookNum = 2和var ChapterNum = 2,而我的目标是要获得第二本书第2章中的经文数量。我将如何编写这样的查询?
编辑:根据评论,我的对象结构也很糟糕,因此我想指出一个正确的结构,使我可以有效地访问大量经文。
答案 0 :(得分:2)
您可以按照下面的方式重写您的无用金额,这将有助于操作
const books = [
{
bookNumber: 1,
bookChapters: 50,
chapter:[
{
chapterNumber: 1,
versesInChapter: 31
},
{
chapterNumber: 2,
versesInChapter: 25
}
],
}
{
bookNumber: 2,
bookChapters: 40,
chapter:[
{
chapterNumber: 1,
versesInChapter: 26
},
{
chapterNumber: 2,
versesInChapter: 22
}
]
}
]
const books = [{
bookNumber: 1,
bookChapters: 50,
chapter: [{
chapterNumber: 1,
versesInChapter: 31
},
{
chapterNumber: 2,
versesInChapter: 25
}
],
},
{
bookNumber: 2,
bookChapters: 40,
chapter: [{
chapterNumber: 1,
versesInChapter: 26
},
{
chapterNumber: 2,
versesInChapter: 22
}
]
}
]
var serach = books.filter(function(book) {
//console.log("books",book);
return book.bookNumber == 1;
})[0];
console.log("Searched book!", serach)
//And for desired result you can use forEach and filter
var num = 2,
chap = 1,
found_chapter;
books.forEach(function(book) {
if (book.bookNumber == num) { //found the book
console.log("found book", book);
found_chapter = book.chapter.filter(function(chapter) {
return chapter.chapterNumber == chap; //found chapter WRT found book
})[0]
}
});
console.log("found chapter-", found_chapter, "--found verse", found_chapter.versesInChapter)
作为上面的代码段,您可以过滤出搜索数据,并可以轻松地再次循环查找所需结果
希望这对您有帮助!
答案 1 :(得分:0)
您可以通过两种方式构造此数据:作为array
或object
。
方法1(对象)
由于书籍被bookNumber
索引,因此bookNumber
成为对象中的键是有意义的。
这可以简化为:
{
bookNumber: {
chapterNumber: versesInChapter,
...
},
...
}
我在代码中添加了一些注释以帮助澄清。
const books = {
1: {
1: 31,
2: 25
},
2 /*bookNumber*/: {
1 /*chapterNumber*/: 26 /*versesInChapter*/ ,
2 /*chapterNumber*/: 22 /*versesInChapter*/
}
};
// es6 style function. Same as `function getVerses(bookNum, chapterNum){...}`
getVerses = (bookNum, chapterNum) => {
if (books[bookNum] && books[bookNum][chapterNum]) {
return books[bookNum][chapterNum];
} else {
// Notify that the index is not set
console.warn('Warning: Book or chapter number is not defined!');
// Return 0 as deafult
return 0;
}
}
let bookNum = 2;
let chapterNum = 2;
let numOfVerses = getVerses(bookNum, chapterNum);
console.log(`Book ${bookNum} chapter ${chapterNum} has ${numOfVerses} verses.`);
方法2(数组)
如果这些书是连续的,则数组也可以工作。
[
[versesInChapter/*1*/,versesInChapter/*2*/, ...],
...
]
例如:
const books = [
[31,25],
[26,22]
];
// es6 style function. Same as `function getVerses(bookNum, chapterNum){...}`
getVerses = (bookNum, chapterNum) => {
bookNum--;
chapterNum--;
if (books[bookNum] && books[bookNum][chapterNum]) {
return books[bookNum][chapterNum];
} else {
// Notify that the index is not set
console.warn('Warning: Book or chapter number is not defined!');
// Return 0 as deafult
return 0;
}
}
let bookNum = 2;
let chapterNum = 2;
let numOfVerses = getVerses(bookNum, chapterNum);
console.log(`Book ${bookNum} chapter ${chapterNum} has ${numOfVerses} verses.`);
希望这会有所帮助,
答案 2 :(得分:0)
您可以将books
和chapters
构造为数组。然后,您可以访问其中的每条数据,例如:
var books = [
{
name: "Book1",
chapters: [
{ countOfVerses: 31 },
{ countOfVerses: 20 }
]
},
{
name: "Book2",
chapters: [
{ countOfVerses: 12 },
{ countOfVerses: 20 },
{ countOfVerses: 16 },
{ countOfVerses: 22 }
]
}
];
for (let book of books){
let name = book.name; // Now we know which book it is...
let chapterCount = book.chapters.length; // ...and how many chapters it has
console.log(`${name} has ${chapterCount} chapters:`)
for (let i = 0; i < chapterCount; i++){
let chapter = book.chapters[i]; // Look in each chapter in the book...
let verses = chapter.countOfVerses; //...and get the countOfVerses property
console.log(` Chapter ${i+1} has ${verses} verses`);
}
}