Javascript多维数组未定义对象错误 [英] Javascript multidimentional array undefined object error

查看:40
本文介绍了Javascript多维数组未定义对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从两个一维数组中创建一个二维数组:

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]
  }

但是,我收到错误消息:'undefined' is null or not an object"并且它指向 for 语句之后的第一行.PassPourcentNames 和 PassPourcentValue 具有相同数量的元素,并且没有一个值为空.第一个包含字符串,第二个包含整数.

However, I get the error message: " 'undefined' is null or not an object " and it points to the first line after the for statement. PassPourcentNames and PassPourcentValue have the same number of elements and none of the values are null. The first one contain strings and the second one integers.

非常感谢任何帮助.

推荐答案

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

也可以使用 []

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

我相信这在大多数 JS 引擎中实际上更快.

I believe this is actually faster in most JS engines.

这篇关于Javascript多维数组未定义对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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