我尝试访问这个多维数组的元素,但结果却很奇怪,如:’o’,’u’。

huangapple go评论157阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月5日 00:31:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75008447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定