jQuery的多维数组 [英] Jquery multidimensional arrays

查看:116
本文介绍了jQuery的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在那里的用户填写的所有信息的一个标志,并将其添加到列表,在那里他可以再添加更多的徽标或删除的应用程序。
想象一下,我添加徽标到列表中包含以下信息:

I'm making an application where the user fills out all the information for a logo and adds it to a list where he can then add more logos or delete them. Imagine I add a logo to the list with the following information:

名称:百事结果
位置:前,后结果
外形尺寸为90mm,60毫米结果
颜色:红色,蓝色,白色结果
选项​​:Whitebg结果
点评:这是一个很酷的标志

Name: Pepsi
Location: Front, Back
Dimensions: 90mm, 60mm
Colors: Red, Blue, White
Options: Whitebg
Comment: This is a cool logo.

该阵列是:

logos[logo[name, loc[], dim[], col[], opt[], com]]

现在我能做到这一点检索一些信息:

Now I can do this to retrieve some info:

logos[0][0] //Prints "Pepsi"
logos[0][1][0] //Prints "Front"
logos[0][2][1] //Prints "60mm"

现在问题来了。当用户完成所有的信息,并添加标识名单我想清空所有的阵列,除了主要的标志之一,所以用户可以添加其他标识到列表中。

Now comes the problem. Whenever the user completes all the info and adds the logo the list I want to empty all the arrays except the main "logos" one so the user can add another logo to the list.

我试图清空标志数组的添加按钮功能的末尾:

I tried to empty the "logo" array at the end of the "add" button function:

logo.length = 0;

但现在的主阵列标志包含一个标志数组女巫是空的。我想保持这些信息在那里。

But now the main array "logos" contains one "logo" array witch is empty. I want to keep that information there.

推荐答案

我想你可以看看这个不同。
我想你应该有一个主要的标志数组。和标识对象。

I think you could look at this differently. I think you should just have a main logos array. And a Logo Object.

function Logo(name,loc, dim, col, opt, com){
    return {
      name:name,
      loc:loc,
      dim:dim,
      col:col,
      opt:opt,
      com:com
    }


}


var logos = [];
logos.push(Logo("blah",somthing[],else[]....);

然后参照:

   logos[0].name;
   logos[0].dimensions[0];

...

您可以添加其他...

you can add another...

 logos.push(Logo("another",....));

另一个选项

像以前一样。

但不是一个标志[]
使用标志= {}对象。

But instead of a Logos[] Use a Logos = {} object.

您可以动态地给定的输入这样的添加属性。

You can dynamically add properties by given input like this.

Logos["First"] = Logo(loc,dim,col,opt,com);
Logos["Second"] = Logo(loc2,dim2,col2,opt2,com2);

如果用户输入他们想要的第一的标志。

If the user inputs that they want the "First" logo.

您可以使用

var firstlogo = Logos["first"];

firstlogo.loc[0] etc.

玩它,使用对象提供了一个更好的了解你正在处理的数据,尤其当多维数组不需要的

Play around with it, using objects provides a better understanding of the data you are dealing with, esp when multidimensional arrays are not "required"

这篇关于jQuery的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆