最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript multidimentional array undefined object error - Stack Overflow

programmeradmin1浏览0评论

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. 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.

Any help is greatly apreciated.

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. 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.

Any help is greatly apreciated.

Share Improve this question asked Nov 9, 2011 at 18:59 sebastien leblancsebastien leblanc 6751 gold badge12 silver badges28 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 1
  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 []

  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.

First define PassAssoArr[k] = []; before assigning to [0] and [1].

Javascript does not support true multi-dimensional arrays.

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[index] = [];   //Empty array literal

You're only defining one dimension of PassAssoArr - you need to set PassAssoArr[k] = new Array();

Try just doing:

PassAssoArr[k] = new Array(PassPourcentNames[k], PassPourcentValue[k]);
发布评论

评论列表(0)

  1. 暂无评论