多维数组给出“错误”的答案

时间:2019-02-02 11:20:43

标签: javascript arrays multidimensional-array

我从编码开始,我有一个多维数组的例子。但是它没有给出预期的答案。

我只得到“ C”,我期望“ JohnnyCash:住在福尔松监狱”。怎么了

var music = []
    music[0] = "Country";
    music[1] = "Rock";
    music[2] = "Punk";
    music[0][0] = "JohnnyCash:Live at Folsom Prison";
    music[0][1] = "PatsyCline:Sentimentally Yours";
    music[0][2] = "HankWilliams:I'm Blue Inside";
    music[1][0] = "T-Rex:Slider";
    music[1][1] = "Nirvana:Nevermind";
    music[1][2] = "Lou Reed:Tranformer";
    music[2][0] = "Flipper:Generic";
    music[2][1] = "TheDeadMilkmen:Big Lizard in my Backyard";
    music[2][2] = "PattiSmith:Easter";

    console.log(music[0][0]);

2 个答案:

答案 0 :(得分:1)

你有

music[0] = "Country";

等到这一行:

music[0][0] = "JohnnyCash:Live at Folsom Prison"

music[0]"Country"(由于之前的分配),因此music[0][0]"C",并且由于字符串是不可变的,因此不允许分配给music[0][0]

如果要在music[0]处有一个数组,则需要在其中放置一个数组,而不是一个字符串。

您可以一次创建该多维结构:

var music = [
    [
        "JohnnyCash:Live at Folsom Prison",
        "PatsyCline:Sentimentally Yours",
        "HankWilliams:I'm Blue Inside"
    ],
    [
        "T-Rex:Slider",
        "Nirvana:Nevermind",
        "Lou Reed:Tranformer"
    ],
    [
        "Flipper:Generic",
        "TheDeadMilkmen:Big Lizard in my Backyard",
        "PattiSmith:Easter"
    ]
];

您需要分别保存类别标签("Country"等)。一种方法是使用对象:

var music = [
    {
        label: "Country",
        entries: [
            "JohnnyCash:Live at Folsom Prison",
            "PatsyCline:Sentimentally Yours",
            "HankWilliams:I'm Blue Inside"
        ]
    },
    {
        label: "Rock",
        entries: [
            "T-Rex:Slider",
            "Nirvana:Nevermind",
            "Lou Reed:Tranformer"
        ]
    },
    {
        label: "Punk",
        entries: [
            "Flipper:Generic",
            "TheDeadMilkmen:Big Lizard in my Backyard",
            "PattiSmith:Easter"
        ]
    }
];

for (var i = 0; i < music.length; ++i) {
  var category = music[i];
  console.log(category.label + ":");
  for (var j = 0; j < category.entries.length; ++j) {
    console.log("* " + category.entries[j]);
  }
}
/* Make the Stack Snippets console full size */
.as-console-wrapper {
  max-height: 100% !important;
}

或使用ES2015 +功能:

const music = [
    {
        label: "Country",
        entries: [
            "JohnnyCash:Live at Folsom Prison",
            "PatsyCline:Sentimentally Yours",
            "HankWilliams:I'm Blue Inside"
        ]
    },
    {
        label: "Rock",
        entries: [
            "T-Rex:Slider",
            "Nirvana:Nevermind",
            "Lou Reed:Tranformer"
        ]
    },
    {
        label: "Punk",
        entries: [
            "Flipper:Generic",
            "TheDeadMilkmen:Big Lizard in my Backyard",
            "PattiSmith:Easter"
        ]
    }
];

for (const category of music) {
  console.log(`${category.label}:`);
  for (const entry of category.entries) {
    console.log(`* ${entry}`);
  }
}
/* Make the Stack Snippets console full size */
.as-console-wrapper {
  max-height: 100% !important;
}

答案 1 :(得分:0)

您已经有了一些有用的解释。您还可以通过这些最小的更改来固定阵列的逐步构建:

var music = []
    music[0] = ["Country"];
    music[1] = ["Rock"];
    music[2] = ["Punk"];
    music[0][0] = "JohnnyCash:Live at Folsom Prison";
    music[0][1] = "PatsyCline:Sentimentally Yours";
    music[0][2] = "HankWilliams:I'm Blue Inside";
    music[1][0] = "T-Rex:Slider";
    music[1][1] = "Nirvana:Nevermind";
    music[1][2] = "Lou Reed:Tranformer";
    music[2][0] = "Flipper:Generic";
    music[2][1] = "TheDeadMilkmen:Big Lizard in my Backyard";
    music[2][2] = "PattiSmith:Easter";

    console.log(music[0][0]);