JavaScript 4D 数组
有人有创建 4D 数组(或任意数量的维度)的函数吗?
我想调用该函数,然后我可以执行类似 arr[3][2][23][12] = "awesome"; 的操作
Anyone have a function to create 4D arrays (or any number of dimensions for that matter)?
I'd like to call the function, then after that I can do something like arr[3][2][23][12] = "awesome";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
快速而肮脏:
查看http://jsfiddle.net/MJg9Y/
注意: 您仍然需要初始化每个维度。上面的代码为
arr[0]
处的 4 维数组创建了基础。Quick and dirty:
Check it out http://jsfiddle.net/MJg9Y/
Note: You will still need to initialize each dimension. The above creates the foundation for a 4 dimension array at
arr[0]
.这是一个简单的递归解决方案。真正的大脑是 mdim 函数。如果深度不为 1,它只会调用自身,并且当它到达那里时,只会返回一个空数组。
因为看起来您可能想将其用于很多事情,所以我将其包装在 Array 的原型中,以便您可以自动在数组上使用它(方便/可维护性权衡)。如果您愿意,可以从闭包中取出 mdim 函数,它应该可以正常工作。
最后有一个简单的测试用例,您可以了解如何使用它。祝你好运! :)
Here's a simple recursive solution. The real brains is the mdim function. It just calls itself if the depth isn't 1, and when it gets there just returns an empty array.
Since it seems like you might want to use this for a lot of things, I've wrapped it in a prototype off of Array so that you can use it on your arrays automatically (convenience/maintainability tradeoff). If you prefer, grab the mdim function out of the closure and it should work just fine.
There's a simple test case at the end so you can see how to use it. Good luck! :)
只需将现有数组中的每个值设置为等于新数组,即可包含所需的元素数量。
请参阅本教程了解一些很好的示例。您可以对任意数量的维度执行此操作。
Just set each value in an existing array equal to a new array, with however many elements you need.
See this tutorial for some good examples. You can do this with any number of dimensions.
更新
修正了之前功能的一些问题;这似乎可以解决问题:
用法
Update
Corrected some issues with the previous function; this seems to do the trick:
Usage
Fwiw,我发布了一个带有三维数组的对象 这里。在那个例子中,
Fwiw, I posted an object with a three dimensional array here. In that example,
非常简单的函数,生成任意维数的数组。指定每个维度的长度和内容,对我来说是 '' 通常
我经常使用这个函数,它节省了很多时间
Very simple function, generate an array with any number of dimensions. Specify length of each dimension and the content which for me is '' usually
I use this function very often, it saves a lot of time
使用以下函数创建和初始化任意维度的数组
例如,要创建 2 行 3 列的数组,请像下面一样使用它
这将创建 require 数组并用“Hello”初始化所有值
类似地创建 4 维数组使用它喜欢
Use the following function to create and initialize Array of any dimension
For e.g to create an array of 2 rows and 3 columns use it like the following
This will create the require array and initialize all values with "Hello"
Similarly to create 4 dimensional array use it like
更新:您可以在第一个参数中指定级别的深度,以及在第二个参数中指定级别的数量。例如:
这将允许您以这种格式设置和获取:
但 X 必须始终小于 64。例如,您不能设置
myMultiArray[X][70][X][X]
,因为myMultiArray[X][70]
尚未定义注意 - 运行
make(64, 4)
非常慢 - 您正在创建 64 ^ 4 个空数组元素(即16,777,216)。更新 2: 您可以将最后一个值设置为任何数字或字符串。 IE。
myMultiArray[X][X][X][Y]
其中 X < 64 和 Y 可以是任何值。算法也优化了,再试试吧。
Update: you can specify how deep a level should be in the first parameter, and how many levels in the second. e.g.:
This will allow you to set and get in this format:
But X must always be less than 64. You cannot set
myMultiArray[X][70][X][X]
for example, becausemyMultiArray[X][70]
has not yet been definedNote- running
make(64, 4)
is awfully slow - you are creating 64 ^ 4 empty array elements (i.e. 16,777,216).Update 2: you can get away with the last value as any number or string. Ie.
myMultiArray[X][X][X][Y]
where X < 64 and Y can be anything.The algorithm has been optimised as well, give it another go.