在JQuery中创建一个数组数组

时间:2014-02-05 07:55:22

标签: javascript arrays multidimensional-array

我想知道如何在JQuery中创建等效的2d数组

从研究中我可以看出JQuery不支持2d数组,但我想知道是否有可能从数组中的数组创建一个。

我想要做的是拥有一个持续数周的数组,并且该数组内部可以容纳7天。

从收集的研究中我似乎想要这样的事情:

{
 "Week":
 [
     {
      "Day":"Monday",
      "Day":"Tuesday",
      "Day":"Wednesday",
      "Day":"Thursday",
      "Day":"Friday",
      "Day":"Saturday",
      "Day":"Sunday"
     }
 ]
}

所以

week[1,2,3,4,5,6,7,8,9,10]
day[Monday,Tuesday, Wednesday, Thursday, Friday,Saturday, Sunday]

如此有效的一周将是行的索引和列的索引的日期。

这实际上可行吗?我习惯了java和jQuery对我来说很奇怪。

3 个答案:

答案 0 :(得分:1)

使用javascript:var a = [[1],[2]]

在数组中的数组

访问数据:a[1][0] -> 2

答案 1 :(得分:1)

我不确定你是否理解你需要做什么。在javascript中,您可以创建数组数组。因此,如果您尝试在一周内存储在数组中,您可以执行此操作:

var weeks = [
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    //...
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
]

console.log(weeks[0][0]); //"Monday"
console.log(weeks[0]); //array["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]

或者您可以像使用2d关联数组一样使用js OBJECT:

var weeks = {
    "week1" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week2" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week3" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
   //...
    "week8" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week9" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
    "week10" : ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"],
}

console.log(weeks.week1[0]); //monday
console.log(weeks.week1); //["Monday",...,"Sunday"]

:)希望它可以提供帮助。

答案 2 :(得分:1)

您可以生成与其他编程语言类似的多个数组。

生成这样一个数组的最短方法是:

[[],[]]

如之前的帖子所述。

但是对于你的问题,我建议你先创建一个这样的日期数组:

var days = ['Monday', 'Tuesday', '...'];

之后你会像这样引用这几天的数组:

var weeks = [days, days, days, days, ...];

这将节省资源,因为始终只有指向同一天数组的指针。 而不是每周的新日数组。

度过愉快的一天。