2016-03-02 37 views
-3

您好使用多维数组变量之前,我开始我也尝试找过关于写作的变量的搜索,如果这已被要求,并回答了那我就道歉,这是莫名其妙我... 。我怎么会去的JavaScript

所以这里去..什么我谈论

var i = e[ab] 
var n = e[cd][ef] 
var t = e[cd][gh] 

我知道,当我想我变种,我可以把e.ab但我怎么会去写变种

例子n和var t

+2

这是一个多维数组,有没有这样的事,作为一个“双变量” –

+0

它(e)是一个数组的数组,这是什么问题? –

+0

也可以是嵌套对象;无论如何,你有一些阅读要做! – Mathletics

回答

0

因此,假如你的对象是这样的(根据您的描述,这听起来像您要访问的对象是另一个对象的属性),并且希望通过索引属性来访问它们(这将是一个财产的财产)。

var e = { 
     ab : "variableOne", 
     cd : {ef:"ef object"}, 
     gh : {ij:"ij object"}, 
    } 

    var i = e["ab"] 
    //if these are properties, then you need to add quotes around them 
    //to access a property through the indexer, you need a string. 
    var n = e["cd"]["ef"] 
    var t = e["gh"]["ij"] 

    console.log(i); 
    console.log(n); 
    console.log(t); 

    console.log("this does the same thing:") 
    console.log(e.ab); 
    console.log(e.cd.ef); 
    console.log(e.gh.if); 

在您的示例中,对象看起来像

//e is the parameter, but I show it as a variable to show 
// it's relation to the object in this example. 

e = { 
    now_playing: {artist:"Bob Seger"; track:"Turn the Page"}} 
} 

这比数组的数组不同:根据JSON :

var arr = [ 
     ['foo','charlie'], 
     ['yip', 'steve'], 
     ['what', 'bob', 'jane'], 
    ]; 


    console.log(arr[0][0]); //foo 
    console.log(arr[0][1]); //charlie 
    console.log(arr[1][0]); //yip 
    console.log(arr[1][1]); //steve 
    console.log(arr[2][2]); //jane 

https://jsfiddle.net/joo9wfxt/2/

EDIT提供,它看起来像参数函数中的0被赋值为数组中的项目的值。与您的代码:

此行显示:“摇滚你喜欢飓风 - Nontas Tzivenis”

$(".song_title .current_show span").html(e.title); 

这行显示:“流氓弗拉茨 - 生命是一个高速公路”。

$(".song_title .current_song span").html(e.np); 

如果它不显示你可能要仔细检查你的jQuery选择。这".song_title .current_song span"is selecting it by the classes on the element

+0

嗨,我已经更新了我的问题,正确的代码,我试图使用旧的网站上工作原代码我只是试图更新它在新的工作任何提示或指针在正确的方向将有助于不寻找任何人为我做的工作只是一头撞在墙上与这几个小时这就是为什么我在这里首先要求在这里感谢您的解释到目前为止 – TheWebMann

+0

你可以发布什么JSON看起来像你从getJson函数? – kemiller2002

+0

已添加json结构示例供您参考 – TheWebMann

0

我觉得你需要一点基本的JavaScript语法复习的。这里是你如何分配一个“空对象”到一个变量,然后开始向它的属性赋值:

e = {} 
e.ab = {} 
e.cd = {} 
e.cd.ef = "data" 

,或者你可以使用属性访问关联数组语法:

e = {} 
e["ab"] = {} 
e["cd"] = {} 
e["cd"]["ef"] = "data" 

你看到后者正在使用对象e就像一个双深度关联数组。那是你想要做的吗?

-1

JavaScript不是强类型。所以数组“a”可以包含不同类型的对象。

var a = [ "a value", [1, 2, 3], function(){ return 5 + 2;}]; 

var result = a[0]; //get the first item in my array: "a value" 
var resultOfIndexedProperty = a[1][0]; //Get the first item of the second item: 1 
var resultOfFunc = a[2](); //store the result of the function that is the third item of my array: 7 

希望这会有所帮助。

+0

这将如何帮助? OP对Javascript类型系统没有困惑。 –

+0

在我看来,他质疑'e [x] [y]'是否会在他的多维阵列中给他(x,y)上的对象。也许我误解了这个问题。 –