简体   繁体   English

创建一个包含多个元素的 JavaScript 数组并嵌套 Arrays

[英]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.我正在尝试分配多个元素并嵌套 arrays 来表示多个 mp3 文件。 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".每个“sermon.bookPart、sermon.file”的“id、书、主题”都是相同的。 For each book the "id, book, and topic" would change (as expected)对于每本书,“id、书和主题”都会改变(如预期的那样)

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我尝试手动填充和分配 arrays,但在尝试执行 3 个嵌套 for 循环以在书 [] arrays 中添加“第 1、2、3 部分”时遇到困难

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>";
    };
};

So you will need to convert the sermon object to another array.因此,您需要将sermon object 转换为另一个数组。

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.然后,您可以使用另一个 for 循环遍历布道。 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, ie 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.你可以做的是创建一个新变量来引用当前的迭代级别,即sermons是最高级别 object,但每个sermon只是 sermons sermons[h]的另一个名称, sermon.book[i]变成book等。 ,这样可以更容易地看到您正在使用的内容。

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>";
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM