Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
Creating a JavaScript Array with Multiple Elements and nested Arrays

I'm trying to assign multiple elements and nested arrays to represent several mp3 files. Each array item will be assigned several unique elements

const sermon = {
    id: "",
    topic: "",
    book: "",
    bookPart: "",
    path: "",
    file: "",
    fileType: "",
    sermonName: function() { 
        return (this.book + " " + this.bookPart).toUpperCase(); 
    }
};

So basically I would like

sermon[0] = { id: "01" topic: "New Testament", book: "Hebrews", bookPart: "Part 1", path: "sermons/" + sermon.book, file: "hebrews_part1.mp3", fileType: "mp3", sermonName: "HEBREWS PART 1"

The "id, book, topic" would be the same for each "sermon.bookPart, sermon.file". For each book the "id, book, and topic" would change (as expected)

It would write to the following:

<h3 id="demo"></h3>
<p id="topic"></p>
<p id="book"></p>
<p id="file"></p>
<p id="path"></p>
document.getElementById("demo").innerHTML = sermon.sermonName();
document.getElementById("topic").innerHTML = sermon.topic;
document.getElementById("book").innerHTML = sermon.book;
document.getElementById("file").innerHTML = sermon.file;
document.getElementById("path").innerHTML = sermon.path;

I've tried to manually populate and assign arrays, but got stuck trying to do 3 nested for loops to add "Part 1,2,3" in with the book[] arrays

let x = "";
const sermon = {
    topic: "New Testament",
    path: "sermons/",
    book: [
        { name: "Hebrews", files: ["hebrews_part1.mp3", "hebrews_part2.mp3", "hebrews_part3.mp3"] },
        { name: "1Corinthians", files: ["1cor_part1.mp3", "1cor_part2.mp3", "1cor_part3.mp3"] }
    ]
};

for (let i in sermon.book) {
    x += "<h3>" + sermon.book[i].name + "</h3>";
    for (let j in sermon.book[i].files) {
        x += "<a href=" + sermon.path + sermon.book[i].name + "/" + sermon.book[i].files[j] + ">" + sermon.book[i].files[j] + "</a><br>";
    };
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So you will need to convert the sermon object to another array.

const sermons =
[
    topic: "New Testament",
    path: "sermons/",
    book: [
        { name: "Hebrews", files: ["hebrews_part1.mp3", "hebrews_part2.mp3", "hebrews_part3.mp3"] },
        { name: "1Corinthians", files: ["1cor_part1.mp3", "1cor_part2.mp3", "1cor_part3.mp3"] }
    ], 
... // add more sermons here
]
};

you can then iterate over the sermons using another for loop. Unfortunately, this is just a result of the structure. What you can do is then create a new variable to reference the current level of iteration, i.e. sermons is the top level object, but each sermon is just another name for sermons[h], sermon.book[i] becomes book, etc., that way it's a little easier to visualize what you're using.

for (let h in sermons) {
    let sermon = sermons[h];
    for (let i in sermon.book) {
        let book = sermon.book[i];
        x += "<h3>" + book.name + "</h3>";
        for (let j in book.files) {
            x += "<a href=" + sermon.path + book.name + "/" + book.files[j] + ">" + book.files[j] + "</a><br>";
        }
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!