简体   繁体   English

将两个一维 arrays 转换为一个二维数组 JavaScript

[英]Transforming two one dimensional arrays into one two dimensional array JavaScript

I have two arrays, both with the same length:我有两个 arrays,长度相同:

  • x = [0, 1, 2, 3, ..., 1439] x = [0, 1, 2, 3, ..., 1439]
  • y = [12, 55, 23, 46, ..., 99] (random Number between 0 and 100) y = [12, 55, 23, 46, ..., 99](0 到 100 之间的随机数)

I wanna combine it to a two dimensional array like this:我想将它组合成这样的二维数组:

  • z = [[0, 12], [1, 55], [2, 23], [3, 46], ..., [1439, 99]] z = [[0, 12], [1, 55], [2, 23], [3, 46], ..., [1439, 99]]

How do I do it?我该怎么做? Nesting, mapping?嵌套,映射? Basically the first array is the key.基本上第一个数组是关键。 In the end I wanna draw a line graph in D3 and all examples I have found use two dimensional arrays.最后我想在 D3 中绘制一个折线图,我发现的所有示例都使用二维 arrays。 One is the x axis, the other the y axis.一个是 x 轴,另一个是 y 轴。 Probably possible another way.可能还有另一种可能。 Thanks谢谢

 let x = [0, 1, 2, 3] let y = [12, 55, 23, 46] let z = []; for(let i=0;i<x.length;i++){ z.push([x[i],y[i]]) } console.log(z)

convert it to 2d array like this像这样将其转换为二维数组

const newArray = []; const x = [0, 1, 2, 3, ..., 1439], y = [12, 55, 23, 46, ..., 99] for(let i = 0; i < x.length; i++){ newArray.push([x[i], y[i]) }

Thanks a lot, that helped.非常感谢,这有帮助。

     let min = 0;
        let max = 100;
        let x_arr = [];
        let y_arr = [];
        let z_arr = [];
        let z_ars = [];
        let z_arx = [];
        let z_arz = [];
        for (let i = 0; i < 360; i++) {
            var r = Math.round(Math.random() * (max - min)) + min;
            x_arr[i]= i;
            y_arr[i]= r;

            z_arr.push([x_arr[i],y_arr[i]]);

            z_ars.push([y_arr[i]]);
            z_ars.sort(function(a, b){return a - b});

            z_arz.push([x_arr[i]]);
            
        }

        z_arx.push([z_arz,z_ars]);

        console.log(z_arr);
        console.log(z_ars);
        console.log(z_arz);
        console.log(z_arx);

Now I wanna sort the random data and put the two arrays together the same way.现在我想对随机数据进行排序,并以同样的方式将两个 arrays 放在一起。 But it is not possible to create them separately and combine them the same way.但是不可能单独创建它们并以相同的方式组合它们。 Something has to be done differently.必须做一些不同的事情。

This almost works but not quite:这几乎可行,但不完全:

            for (let i = 0; i < 360; i++) {
            var r = Math.round(Math.random() * (max - min)) + min;
            x_arr[i]= i;
            y_arr[i]= r;
            z_arr.push([x_arr[i],y_arr[i]]);

            s_arr = y_arr.sort(function(a, b){return a - b});

            komplett.push([x_arr[i],s_arr[i]]);
        }

        console.log(z_arr);
        console.log(s_arr);
        console.log(komplett);

This did the sorting:这进行了排序:

  s_arr = y_arr.sort(function(a, b){return a - b});
  let neu_arr = [];
  let zz_arr = [];
  for (let i = 0; i < 1440; i++) {
    neu_arr[i]= i;
    zz_arr.push([neu_arr[i], s_arr[i]]);
  }
  document.write("zz " + zz_arr + '<hr>');
  console.log(zz_arr);´

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

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