需要帮助建立复杂的JS对象 [英] Need help building complex JS object

查看:96
本文介绍了需要帮助建立复杂的JS对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建在JavaScript中的数组,但我不知道正确的方式去做,如果此类型的数组甚至可以...

I'm trying to construct an array in JavaScript, but I'm not sure of the correct way to do it, or if this type of array is even possible...

可以说我有每个项目的数组中以'A'和'Z'结尾的关键。对于每个项目,数组键将与另一个多维数组对应。这些新的多维数组是一系列坐标(x和y)的。原始数组中的每一项都可以拥有多套坐标。例如:

Lets say I have a key for each item in the array starting with 'a' and ending with 'z'. For each item, the array key will correspond with another multidimensional array. These new multidimensional arrays are a series of coordinates (x and y). Each item in the original array can have many sets of coordinates. For example:

我如何构建使用JavaScript这样的阵列?什么是正确的语法?

How can I construct such an array with Javascript? What is the proper syntax?

推荐答案

有关您所提供的数据:

var arr = {
  a: [[20,15],[25,17],[10,45]],
  b: [[10,33],[12,2],[14,9],[72,103],[88,12]],
  c: [[2,2],[41,21]],
  d: [[0,0],[21,2],[44,44],[19,99],[1,1],[100,100]],
  e: [[1,1],
  f: [[3,40],[41,86]]
}

您想第一个结构,键控阵,是不是在JavaScript中的数组 - 它是一个对象。对象包含键 - 值对。在这种情况下,值是数组,并且在这些阵列的对象是自己的阵列

The first structure you want, a keyed array, isn't an array in JavaScript - it's an object. Objects contain key-value pairs. In this case, the values are arrays, and the objects in those arrays are themselves arrays.

这是重要的事情,这里要注意,如果你从定义常规多维数组是没有预期或保证这种结构的行都是一样的长度另一种语言来。

An important thing to note here if you're coming from another language that defines 'regular' multi-dimensional arrays is that there is no expectation or guarantee that the 'rows' of this structure are all the same length.

这篇关于需要帮助建立复杂的JS对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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