英文:
I am trying to access elements of this multidimensional array and getting strange results like : 'o','u'
问题
BestAlbumsByGenre = [];
BestAlbumsByGenre [0] = "Country";
BestAlbumsByGenre [0][0] = "Johnny Cash : Live at Folsom Prison";
BestAlbumsByGenre [0][1] = "Patsy Cline : Sentimentally Yours";
BestAlbumsByGenre [0][2] = "Hank Williams : I'm Blue Inside";
BestAlbumsByGenre [1] = "Rock";
BestAlbumsByGenre [1][0] = "T-Rex : Slider";
BestAlbumsByGenre [1][1] = "Nirvana : Nevermind";
BestAlbumsByGenre [1][2] = "Lou Reed : Transformer";
BestAlbumsByGenre [2] = "Punk";
BestAlbumsByGenre [2][0] = "Flipper : Generic";
BestAlbumsByGenre [2][1] = "The Dead Milkmen";
BestAlbumsByGenre [2][2] = "Patti Smith : Easter";
console.log(BestAlbumsByGenre[0][1]);
英文:
let BestAlbumsByGenre = [];
BestAlbumsByGenre [0] = "Country";
BestAlbumsByGenre [0][0] = "Johnny Cash : Live at Folsom Prison";
BestAlbumsByGenre [0][1] = "Patsy Cline : Sentimentally Yours";
BestAlbumsByGenre [0][2] = "Hank Williams : I'm Blue Inside";
BestAlbumsByGenre [1] = "Rock";
BestAlbumsByGenre [1][0] = "T-Rex : Slider";
BestAlbumsByGenre [1][1] = "Nirvana : Nevermind";
BestAlbumsByGenre [1][2] = "Lou Reed : Transformer";
BestAlbumsByGenre [2] = "Punk";
BestAlbumsByGenre [2][0] = "Flipper : Generic";
BestAlbumsByGenre [2][1] = "The Dead Milkmen";
BestAlbumsByGenre [2][2] = "Patti Smith : Easter";
console.log(BestAlbumsByGenre[0][1]);
this is my code.
If I want to access the BestAlbumsByGenre[0][1]
, it should return "Patsy Cline : Sentimentally Yours";
But it's returning the character 'o', maybe it's returning the 2nd character of 0th element, String "Country", but if it so then why?
thanks in advance.
答案1
得分: 1
在BestAlbumsByGenre [0] = "Country"
之后,将一个字符串设置为数组的第0个条目之后,BestAlbumsByGenre [0][0] = "Johnny Cash : Live at Folsom Prison"
不再起作用。BestAlbumsByGenre[0][1]
返回该字符串的第一个字符。
考虑使用不同的数据结构,例如:
BestAlbumsByGenre[0] = {Genre: "Country", Albums: []};
BestAlbumsByGenre[0].Albums[0] = "Johnny Cash : Live at Folsom Prison";
英文:
After BestAlbumsByGenre [0] = "Country"
, which sets a string as 0th entry in the array, BestAlbumsByGenre [0][0] = "Johnny Cash : Live at Folsom Prison"
has no effect any more. BestAlbumsByGenre[0][1]
returns the 1st character in that string.
Consider a different data structure, for example:
BestAlbumsByGenre[0] = {Genre: "Country", Albums: []};
BestAlbumsByGenre[0].Albums[0] = "Johnny Cash : Live at Folsom Prison";
答案2
得分: 1
我不认为你完全理解JavaScript中的多维数组结构。
这个示例是否能让它们的结构更清晰?
let BestAlbumsByGenre = [];
BestAlbumsByGenre[0] = [
"Country", // 0 0
[ // 0 1
"Johnny Cash : Live at Folsom Prison", // 0 1 0
"Patsy Cline : Sentimentally Yours", // 0 1 1
"Hank Williams : I'm Blue Inside" // 0 1 2
]
]; //等等...
console.log(BestAlbumsByGenre[0][0]); //Country
console.log(BestAlbumsByGenre[0][1][1]); //Patsy Cline : Sentimentally Yours
英文:
I don't think you understand multidimensional array structures in javascript at all.
Does this example make their structure clearer?
let BestAlbumsByGenre = [];
BestAlbumsByGenre[0] = [
"Country", // 0 0
[ // 0 1
"Johnny Cash : Live at Folsom Prison", // 0 1 0
"Patsy Cline : Sentimentally Yours", // 0 1 1
"Hank Williams : I'm Blue Inside" // 0 1 2
]
]; //etc...
console.log(BestAlbumsByGenre[0][0]); //Country
console.log(BestAlbumsByGenre[0][1][1]); //Patsy Cline : Sentimentally Yours
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论