简体   繁体   English

将JavaScript数组添加到多维数组

[英]Add javascript array to a multidimensional array

I have created an empty array, and I'd like to create and append arrays within this array: 我创建了一个空数组,我想在此数组中创建和追加数组:

values: []

I want to have the values array in a format like this: 我想以这样的格式获取值数组:

[ [ 1025409600000 , 23.041422681023] , [ 1028088000000 , 19.854291255832] , [ 1030766400000 , 21.02286281168] ]

So I did 所以我做了

values.push({x: 1025409600000, y: 23.041422681023});

This creates a javascript object instead of an array of arrays. 这将创建一个javascript对象,而不是一个数组数组。 How can I not append a new javascript object but append an array within the values array instead? 我如何不能附加一个新的JavaScript对象,而是在values数组内附加一个数组?

只是推数组=)

values.push([1025409600000, 23.041422681023]);

just iterate through your list of x and y coordinates and push them into their own array and then push each of those arrays into the parent (values) array. 只需遍历您的x和y坐标列表,然后将它们推入自己的数组中,然后将每个数组推入父(值)数组即可。 The following is obviously wrong and you need to iterate via loops, but this will give you an array called values, which is made up of other arrays of x and y coordinates. 以下内容显然是错误的,您需要遍历循环,但这将为您提供一个名为values的数组,该数组由x和y坐标的其他数组组成。

var values = [];
var coordinates=[];
var x=1025409600000;
var y=23.041422681023;

coordinates.push(x);
coordinates.push(y);
//leads to coordinates being ['1025409600000','23.041422681023']

values.push(coordinates);

//leads to values being [['1025409600000 ','23.041422681023']]

and then you repeat to ensure that values is [[x,y],[x,y],[x,y]...] 然后重复以确保值是[[x,y],[x,y],[x,y] ...]

This is because you're pushing objects {} into the array. 这是因为您要将对象{}推入数组。 You need to push arrays, so something like this: 您需要推送数组,因此如下所示:

values.push([obj.x, obj.y]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM