I have a JSON file containing a bunch of songs with the associated song lines, verse number etc. My goal is to print each distinct verse. I'm having difficulty constructing a loop that works. Here is a sample of the JSON file:
[{ "song": 1, "verse": 1, "lineNum": 1, "line": "Mato Joe Tsinki"},
{ "song": 1, "verse": 1, "lineNum": 2, "line": "Chiti chiti"},
{ "song": 1, "verse": 2, "lineNum": 1, "line": "Raro Raro"},
{ "song": 1, "verse": 2, "lineNum": 2, "line": "Shinan Kibi"},
{ "song": 1, "verse": 2, "lineNum": 3, "line": "Bewa bewa"},
{ "song": 2, "verse": 1, "lineNum": 1, "line": "Ramo Kano"},
{ "song": 2, "verse": 2, "lineNum": 1, "line": "Choro Choro"},
{ "song": 3, "verse": 1, "lineNum": 1, "line": "Pisha Pisha"}]
In this JSON there are 5 verses total.
- Song 1, Verse 1: "Mato Joe Tsinki" / "Chiti Chiti"
- Song 1, Verse 2: "Raro Raro" / "Shinan Kibi" / "Bewa Bewa"
- Song 2, Verse 1: "Ramo Kano"
- Song 2, Verse 2: "Choro Choro"
- Song 3, Verse 1: "Pisha Pisha"
I've written the following which successfully prints the 1st verse of every song but not subsequent verses.
var fs = require('fs');
var colors = require('colors');
fs.readFile('stackoverflow.json', 'utf8', function (err,data) {
data = JSON.parse(data);
for(var i in data) {
var item = data[i];
// construct string song:verse:line
var lineIndex = item.song
lineIndex += ":"+item.verse
lineIndex += ":"+item.lineNum
var numSongs = 20
var songNum = 1
var verseNum = 1
var lineNum = 1
// console.log(verseNum)
while(item.song <= numSongs && item.verse == verseNum)
{
console.log(lineIndex.green + ' ' + item.line);
verseNum++;
}
}
});
Updated to demonstrate desired output a console log:
Song 1 Verse 1: "Mato Joe Tsinki" "Chiti Chiti"
Song 1 Verse 2: "Raro Raro" "Shinan Kibi" "Bewa Bewa"
Song 2 Verse 1: ....