0

我对数组还是很陌生,我的问题是你如何一个接一个地链接数组?

例如:在下面的season数组中,有4个key/value,那么在designer0designer1数组中,有3个key/value对。

对于designer0数组,它属于season[0],而对于designer1数组,它属于season[1],但是我如何以这样的方式编写它,使之有父子关系?

var season = new Array();
season[0] = "summer 2013";
season[1] = "winter 2014";
season[2] = "autumn 1998";
season[3] = "spring 2005";


var designer0 = new Array();
designer[0] = "Albbs";
designer[1] = "Alexander Wang";
designer[2] = "BCBG Max Azria";


var designer1 = new Array();
designer[0] = "William Howe";
designer[1] = "Bringingthewood";
designer[2] = "Kesha";
4

4 回答 4

2

您需要一个新类型:“对象”,就像键和值一样。

var seasons = [{
    name: "summer 2013",
    designers: ["Albbs", "Alexander Wang", "BCBG Max Azria"]
}, {
    name: "winter 2014",
    designers: ["William Howe", "Bringingthewood", "Kesha"]
}];

对象数组。

于 2013-11-13T06:35:01.280 回答
1

如果我们谈论的是 javascript,您应该记住:其中没有原生关联数组。由于您season已经包含数据,因此保持从属关系的唯一方法是创建关联数组,其中季节名称将是一个键,设计师数据 - 一个值。

但是,您可以使用对象数组来创建所需的数据结构:

var seasons = [
    {
       "index"    : 0, 
       "name"     : 'summer 2013',
       "designers": ['Albbs', 'Alexander Wang']
    },
    {
       "index"    : 1, 
       "name"     : 'winter 2014',
       "designers": ['William Howe', 'Kesha']
    }
];

-看这个小提琴来检查如何处理这些数据。

于 2013-11-13T06:32:53.273 回答
1

您可以使用 JSON:

  [
    {
    "season":"summer 2013",
    "designer1":"Albbs",
    "designer2":"William Howe"
    },
    {
    "season":"winter 2014",
    "designer1":"Alexander Wang",
    "designer2":"Bringingthewood"
    }
 ]
于 2013-11-13T06:35:32.170 回答
0

添加一个链接它们的新数组,然后引用该数组:

seasonal_designers =
[
0 = {designer0[0],designer0[1],designer0[2]}
1 = {designer1[0],designer1[1],designer1[2]}
]

现在得到你的东西是这样的:

value=''
for(i in seasonal_designers[specify number])
value+=seasonal_designers[specify number][i] + ' (the designers id '+ i +')'
于 2013-11-13T06:38:38.587 回答