如何在JavaScript中制作3D对象数组?

时间:2012-11-01 15:10:48

标签: javascript

lyricsInp = document.getElementById("lyrics").value;
var lines = lyricsInp.split("\n");
for (i = 0; i < lines.length; i++) {
    holder[0][i] = new Array();
    words = lines[i].replace(/[ \t\r]+/g, "###").split("###");
    for (j = 0; j < words.length; j++) {
        holder[0][i][j].word = words[j];
        holder[0][i][j].startT = 0;
        holder[0][i][j].endT = 0;
    }
}

在这里,我需要每个持有人元素保留word, startTendT,但这不起作用。我该如何实现这一目标。

3 个答案:

答案 0 :(得分:1)

holder[0][i][j]声明为对象文字:

for (j = 0; j < words.length; j++) {
    holder[0][i][j] = {};

    holder[0][i][j].word = words[j];
    holder[0][i][j].startT = 0;
    holder[0][i][j].endT = 0;
}

答案 1 :(得分:0)

lyricsInp =  document.getElementById("lyrics").value;
var lines = lyricsInp.split("\n");
for (i=0;i<lines.length;i++){
    holder[0][i] = new Array();
    words = lines[i].replace(/[ \t\r]+/g,"###").split("###");
    for (j=0;j<words.length;j++){
        holder[0][i][j] = {
                word: words[j],
                startT: 0,
                endT: 0
            };
    }
}

答案 2 :(得分:0)

var holder = [[]];

var lyricsInp = document.getElementById("lyrics").value,
    lines = lyricsInp.split("\n");
for (var i = 0; i < lines.length; i++) {
    holder[0][i] = [];
    var words = lines[i].replace(/[ \t\r]+/g, "###").split("###");
    for (var j = 0; j < words.length; j++) {
        holder[0][i][j] = {
            word: words[j],
            startT: 0,
            endT: 0
        };
    }
}