在 javascript 中创建和解析 3D 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4943633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:53:48  来源:igfitidea点击:

creating and parsing a 3D array in javascript?

javascript

提问by kiran

can any one help me to create and parse a 3d array in javascript.

任何人都可以帮助我在 javascript 中创建和解析 3d 数组。

im having a questionId each question Id have a selected option and an optional option text.

我有一个 questionId 每个问题 Id 都有一个选定的选项和一个可选的选项文本。

so i need to create a 3d array for questionId,optionId,optionText(string)..

所以我需要为 questionId、optionId、optionText(string) 创建一个 3d 数组。

Thnks in advance

提前谢谢

回答by Tom Gullen

A one dimension array would be:

一维数组将是:

var myArr = new Array();
myArr[0] = "Hi";
myArr[1] = "there";
myArr[2] = "dude";

alert(myArr[1]);    // Alerts 'there'

A two dimensional array would be:

二维数组将是:

var myArr = new Array();
myArr[0] = new Array("Val", "Va");
myArr[1] = new Array("Val", "yo");
myArr[2] = new Array("Val", "Val");

alert(myArr[1][1]); // Alerts 'yo'

A 3D array is more complicated, and you should evaluate your proposed use of it as it has a limited scope within the web. 99% of problems can be solved with 1D or 2D arrays. But a 3D array would be:

3D 数组更复杂,您应该评估您对它的建议使用,因为它在网络中的范围有限。99% 的问题都可以用一维或二维数组解决。但是 3D 数组将是:

var myArr = new Array();
myArr[0] = new Array();
myArr[0][0] = new Array()
myArr[0][0][0] = "Howdy";
myArr[0][0][1] = "pardner";

alert(myArr[0][0][1]); // Alerts 'pardner'

回答by Joe Green

var data = new Array();

data starts off as a regular one-dimensional array. A two dimensional array is really just an array of arrays. So you could do something like

数据从一个常规的一维数组开始。二维数组实际上只是数组的数组。所以你可以做类似的事情

for (var i = 0; i<numberOfQuestions; i++){
 data[i] = new Array();
 data[i][0] = something;
 data[i][1] = somethingElse;
}

Alternatively you could use the following approach

或者,您可以使用以下方法

for (var i = 0; i<numberOfQuestions; i++){
 data.push([something, somethingElse]);
}

In any case, at some point you are going to need a loop to populate your initial array with sub-arrays to get the 2d effect.

在任何情况下,在某些时候,您将需要一个循环来使用子数组填充初始数组以获得 2d 效果。

回答by usoban

Okay, so as I said in comment to the question, I think you actually need 2D array. That is, if you have N number of questionIds and each questionId has two properties: optionId and optionText

好的,正如我在对该问题的评论中所说的那样,我认为您实际上需要二维数组。也就是说,如果你有 N 个 questionIds 并且每个 questionId 有两个属性:optionId 和 optionText

You can create it somehow like this:

你可以像这样创建它:

var twoD = new Array();

twoD[1] = new Array();
twoD[1]['optionId'] = 3;
twoD[1]['optionText'] = 'blabla';

twoD[2] = new Array();
twoD[2]['optionId'] = 5;
twoD[2]['optionText'] = null;

alert(twoD[1]['optionId']);

Although note that array with associative key is actually an object in JavaScript.

尽管请注意带有关联键的数组实际上是 JavaScript 中的一个对象。

Update: looping through JavaScript

更新:通过 JavaScript 循环

for(var question : twoD){
 alert(question['optionId']);
 alert(question['optionText']);
}

回答by Ankur

hi you can create the nth dimension error in javascript one of the example is below

嗨,您可以在 javascript 中创建第 n 维错误示例之一如下

var apple = new Array(); apple[1] = new Array(); apple[1][1] = new Array(); apple[1][1][1] = 'yourname'

var apple = new Array(); 苹果[1] = 新数组(); 苹果[1][1] = new Array(); 苹果[1][1][1] = '你的名字'

now you can use the recursive function to iterate through the array and check and pare every thing.

现在您可以使用递归函数遍历数组并检查和删除所有内容。