te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>Creating a 2d array of objects in javascript - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Creating a 2d array of objects in javascript - Stack Overflow

programmeradmin4浏览0评论

I have a javascript object -

cell{xPos, yPos};

I would like to create a 2d array of this object.

cellPrototype = function(x, y) {
this.xPos = x;
this.yPos = y;
}
var cell = new Array();
for(var i=0;i<10;i++)
{
  cell[i] = new Array();
  for(var j=0;j<10;j++)
  {
     cell[i][j] = new cellPrototype(i,j);
  }
}

This code doesn't work. Neither does -

var cellPrototype = function(x, y) {    
return { 
  xPos : x;
  yPos : y;
}
var cell = new Array();
for(var i=0;i<10;i++)
{
  cell[i] = new Array();
  for(var j=0;j<10;j++)
  {
     cell[i][j] = new cellPrototype(i,j);
  }
}

So how do I create a 2d array of an object in javascript?

I have a javascript object -

cell{xPos, yPos};

I would like to create a 2d array of this object.

cellPrototype = function(x, y) {
this.xPos = x;
this.yPos = y;
}
var cell = new Array();
for(var i=0;i<10;i++)
{
  cell[i] = new Array();
  for(var j=0;j<10;j++)
  {
     cell[i][j] = new cellPrototype(i,j);
  }
}

This code doesn't work. Neither does -

var cellPrototype = function(x, y) {    
return { 
  xPos : x;
  yPos : y;
}
var cell = new Array();
for(var i=0;i<10;i++)
{
  cell[i] = new Array();
  for(var j=0;j<10;j++)
  {
     cell[i][j] = new cellPrototype(i,j);
  }
}

So how do I create a 2d array of an object in javascript?

Share Improve this question asked Jun 28, 2011 at 17:48 elssarelssar 5,8717 gold badges48 silver badges75 bronze badges 3
  • 1 Unless I've misunderstood your question, your first example seems to work fine: jsfiddle/tgz22 – James Allardice Commented Jun 28, 2011 at 17:51
  • What doesn't work? The second one at least give me an array with 10 arrays of 10 cellPrototypes each... – josh.trow Commented Jun 28, 2011 at 17:52
  • @James @josh.trow Don't know what happened. I have been testing this code on my puter for the last hour or two trying to make out what went wrong. After fiddling in jsFiddle for a bit I got it to work. Dunno, maybe some small typo somewhere was the problem. Everyone who answered & mented, thank you for your help. – elssar Commented Jun 28, 2011 at 18:32
Add a ment  | 

4 Answers 4

Reset to default 7

This works fine for me, I'm not sure if that's exactly the output you're looking for, where Array[x][y] will reference an object with points at x, y.

var Coords = function(x, y) {
    return {
        "x" : x,
        "y" : y
    };
};

var Main = [];

for (var i = 0, l = 10; i < l; i++) {
    Main[i] = [];
    for (var j = 0, l2 = 10; j < l2; j++) {
        Main[i][j] = Coords(i, j);
    }
}

http://jsfiddle/robert/d9Tgb/

You can make a 2d array like so:

var new_array = [];
var arr_length = 10;
for(var i = 0; i < arr_length; ++i){
    new_array[i] = [];
}

This post is a bit old, but here is another way to create a 2D array

var arr1=[];
var x=['a','b','c','d'];
var y=[1,2,3,4];     

for( var i in x){
    arr1.push([x[i],y[i]]); //For x and y of the same length
}

In JavaScript x and y can be objects arrays

jsFiddle It :)

make an empty array and push the child arrays onto it

var array = [];
array.push([1,2,3,4]);
//array[0][0] == 1

or all in one shot

var array = [[1,2,3,4], [1,2,3,4], [1,2,3,4]];
发布评论

评论列表(0)

  1. 暂无评论