This file splits the text variable from book, chapter, and verses. It should ideally look like this:
[
{
"book": "PLACEHOLDER",
"chapters": [
{
"chapter": 1,
"verses": [
{
"verse": 1,
"text": "this is book 1, chapter 1, verse 1"
},
{
"verse": 2,
"text": "this is book 1, chapter 1, verse 2"
}
]
},
{
"chapter": 1,
"verses": [
{
"verse": 1,
"text": "this is book 1, chapter 2, verse 1"
},
{
"verse": 2,
"text": "this is book 1, chapter 2, verse 2"
}
]
}
]
},
{
"book": "PLACEHOLDER",
"chapters": [
{
"chapter": 1,
"verses": [
{
"verse": 1,
"text": "this is book 2, chapter 1, verse 1"
},
{
"verse": 2,
"text": "this is book 2, chapter 1, verse 2"
}
]
},
{
"chapter": 2,
"verses": [
{
"verse": 1,
"text": "this is book 2, chapter 2, verse 1"
},
{
"verse": 2,
"text": "this is book 2, chapter 2, verse 2"
}
]
}
]
}
]
Instead, I got this, which duplicated the book but moved on to the next chapter:
[
{
"book": "PLACEHOLDER",
"chapters": [
{
"chapter": 1,
"verses": [
{
"verse": 1,
"text": "this is book 1, chapter 1, verse 1"
},
{
"verse": 2,
"text": "this is book 1, chapter 1, verse 2"
}
]
},
{
"chapter": 1,
"verses": [
{
"verse": 1,
"text": "this is book 1, chapter 1, verse 1"
},
{
"verse": 2,
"text": "this is book 1, chapter 1, verse 2"
}
]
}
]
},
{
"book": "PLACEHOLDER",
"chapters": [
{
"chapter": 2,
"verses": [
{
"verse": 1,
"text": "this is book 1, chapter 2, verse 1"
},
{
"verse": 2,
"text": "this is book 1, chapter 2, verse 2"
}
]
},
{
"chapter": 2,
"verses": [
{
"verse": 1,
"text": "this is book 1, chapter 2, verse 1"
},
{
"verse": 2,
"text": "this is book 1, chapter 2, verse 2"
}
]
}
]
}
]
let text = `
this is book 1, chapter 1, verse 1.
this is book 1, chapter 1, verse 2.
*
this is book 1, chapter 2, verse 1.
this is book 1, chapter 2, verse 2.
#
this is book 2, chapter 1, verse 1.
this is book 2, chapter 1, verse 2.
*
this is book 2, chapter 2, verse 1.
this is book 2, chapter 2, verse 2.
`;
let volume = [];
let book = text.split("#");
book.forEach((bookText) => {
let chapter = bookText.split("*");
chapter.forEach((chapterText, chapterIndex) => {
let verse = chapterText.split(".");
volume.push({
book: "PLACEHOLDER",
chapters: chapter
.filter((chapter) => chapter.trim() !== "")
.map(() => ({
chapter: chapterIndex + 1,
verses: verse
.filter((verse) => verse.trim() !== "")
.map((verse, verseIndex) => ({
verse: verseIndex + 1,
text: verse.trim() + "",
})),
})),
});
});
});
console.log(JSON.stringify(volume, null, 2));
Is there a possibility that it is the double map method and or the double forEach method? I tried removing either of them but would give me errors like (bookText.split is not a method and chapterText.split is not a method.)