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; } ?>javascript - Why can't I make a copy of this 2d array in JS? How can I make a copy? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why can't I make a copy of this 2d array in JS? How can I make a copy? - Stack Overflow

programmeradmin3浏览0评论

I'm implementing a John Conway Game of Life, but I'm having a weird problem. Here is a short version if the code giving me trouble:

let lifeMap = [
  [true, false, false],
  [false, false, false],
  [false, false, false]
];
let oldLifeMap = lifeMap.slice();
for (let row = 0; row < lifeMap.length; row++) {
  for (let val = 0; val < lifeMap[row].length; val++) {
    let bool = lifeMap[row][val];
    let newBool = false; // here is where I would determine if cell is alive/dead
    lifeMap[row][val] = newBool;
    if (row === 0 && val === 0) console.log("at (0,0)", oldLifeMap[0][0]);
  }
}

In response to this code, JavaScript prints at (0,0) false. I need it to stay true until the next generation starts.

I thought doing let oldLifeMap = lifeMap.slice() would fix it, but it doesn't, and I'm not sure why. (Shouldn't it copy the 2d array and not create a second ref to it?)

Anyway, what is going on here, and how do I successfully make an actual copy of lifeMap here?

I'm implementing a John Conway Game of Life, but I'm having a weird problem. Here is a short version if the code giving me trouble:

let lifeMap = [
  [true, false, false],
  [false, false, false],
  [false, false, false]
];
let oldLifeMap = lifeMap.slice();
for (let row = 0; row < lifeMap.length; row++) {
  for (let val = 0; val < lifeMap[row].length; val++) {
    let bool = lifeMap[row][val];
    let newBool = false; // here is where I would determine if cell is alive/dead
    lifeMap[row][val] = newBool;
    if (row === 0 && val === 0) console.log("at (0,0)", oldLifeMap[0][0]);
  }
}

In response to this code, JavaScript prints at (0,0) false. I need it to stay true until the next generation starts.

I thought doing let oldLifeMap = lifeMap.slice() would fix it, but it doesn't, and I'm not sure why. (Shouldn't it copy the 2d array and not create a second ref to it?)

Anyway, what is going on here, and how do I successfully make an actual copy of lifeMap here?

Share Improve this question asked Aug 29, 2017 at 22:31 Andrew HornAndrew Horn 2,5792 gold badges17 silver badges26 bronze badges 11
  • The outer array is a copy, but each inner array remains a reference. That is expected behavior from what you're doing. – Patrick Roberts Commented Aug 29, 2017 at 22:38
  • gotcha. How do I make a deep copy then? – Andrew Horn Commented Aug 29, 2017 at 22:41
  • 3 .map(a => a.slice()) – Patrick Roberts Commented Aug 29, 2017 at 22:43
  • Possible duplicate of How do you clone an Array of Objects in Javascript? – abagshaw Commented Aug 29, 2017 at 22:43
  • 1 var copy = JSON.parse(JSON.stringify(lifeMap)) – epascarello Commented Aug 29, 2017 at 22:48
 |  Show 6 more ments

2 Answers 2

Reset to default 13

A hat-tip to @Redu's answer which is good for N-dimensional arrays, but in the case of 2D arrays specifically, is unnecessary. In order to deeply clone your particular 2D array, all you need to do is:

let oldLifeMap = lifeMap.map(inner => inner.slice())

This will create a copy of each inner array using .slice() with no arguments, and store it to a copy of the outer array made using .map().

You may clone an ND (deeply nested) array as follows;

Array.prototype.clone = function(){
  return this.map(e => Array.isArray(e) ? e.clone() : e);
};

or if you don't want to modify Array.prototype you may simply refactor the above code like;

function cloneArray(a){
  return a.map(e => Array.isArray(e) ? cloneArray(e) : e);
};
发布评论

评论列表(0)

  1. 暂无评论