简体   繁体   English

Javascript multidimentional数组未定义的对象错误

[英]Javascript multidimentional array undefined object error

I am trying to make a two dimensional array out of two one dimentional arrays with this code: 我试图用这个代码从两个一维数组中制作一个二维数组:

  var PassAssoArr = new Array();
  for(k in PassPourcentNames) {
    PassAssoArr[k][0] = PassPourcentNames[k]
    PassAssoArr[k][1] = PassPourcentValue[k]
  }

However, I get the error message: " 'undefined' is null or not an object " and it points to the first line after the for statement. 但是,我收到错误消息:“'undefined'为null或不是对象”,它指向for语句后的第一行。 PassPourcentNames and PassPourcentValue have the same number of elements and none of the values are null. PassPourcentNames和PassPourcentValue具有相同数量的元素,并且没有值为null。 The first one contain strings and the second one integers. 第一个包含字符串,第二个包含整数。

Any help is greatly apreciated. 任何帮助都是非常有用的。

First define PassAssoArr[k] = []; 首先定义PassAssoArr[k] = []; before assigning to [0] and [1] . 在分配到[0][1]

Javascript does not support true multi-dimensional arrays. Javascript不支持真正的多维数组。

You're trying to use nested arrays without creating the inner arrays. 您正在尝试使用嵌套数组而不创建内部数组。

You need to put an array into each element of the outer PassAssoArr : 您需要将一个数组放入外部PassAssoArr每个元素中:

PassAssoArr[index] = [];   //Empty array literal

你只定义了PassAssoArr的一个维度 - 你需要设置PassAssoArr [k] = new Array();

  var PassAssoArr = new Array();
  for(k in PassPourcentNames) {
    PassAssoArr[k] = new Array();
    PassAssoArr[k][0] = PassPourcentNames[k]
    PassAssoArr[k][1] = PassPourcentValue[k]
  }

Also instead of new Array() you can use [] 而不是new Array()你可以使用[]

  var PassAssoArr = [];
  for(k in PassPourcentNames) {
    PassAssoArr[k] = [];
    PassAssoArr[k][0] = PassPourcentNames[k]
    PassAssoArr[k][1] = PassPourcentValue[k]
  }

I believe this is actually faster in most JS engines. 我相信这在大多数JS引擎中实际上更快。

试着做:

PassAssoArr[k] = new Array(PassPourcentNames[k], PassPourcentValue[k]);

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

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