简体   繁体   English

从其他二维数组 Javascript 的部分创建二维数组

[英]Creating 2d array from section of other 2d array Javascript

I am trying to cut a section of a 2d array specified by x, y and w, h .我正在尝试剪切由x, yw, h指定的二维数组的一部分。 an example of an array I'm trying to cut would be我试图切割的数组的一个例子是


var arr = [
            ['0', '1', 'd', 'a', '1', '1'],
            ['0', 's', '0', 'f', 's', 'g'],
            ['b', 'x', 'e', '0', 'v', 'a'],
            ['a', 'e', 'n', '0', 'z', 'm'],
            ['b', 'x', 'e', '0', 'v', 'a'],
         ];

so if i called snapshot(2, 1, 4, 2) my desired output would be所以如果我调用snapshot(2, 1, 4, 2)我想要的 output 将是

var retarr = [
                 ['0', 'f', 's', 'g'],
                 ['e', '0', 'v', 'a'],
             ]

So far I can cut the section and return a new array successfully but only if my W idth and H eight are equal.到目前为止,我可以切割部分并成功返回一个新数组,但前提是我的W idth 和H八相等。

snapshot(x, y, w, h){
    var retgrid = new Grid(w, h); 
    var startX = x;
    var startY = y;
    var endX = x + w;
    var endY = y + h;
    console.log(`x: ${x} y: ${y}`)
    console.log(`w: ${w} h: ${h}`)

    for(var i = startY; i < endY; i++){
        for(var j = startX; j < endX; j++){
           // console.log(retgrid)
            retgrid[j - startX][i - startY] = this.grid[j][i]
        }
    }
    console.log(retgrid)
    return retgrid;
}

an error that keeps occurring is game.js:316 Uncaught TypeError: Cannot set property '-1' of undefined不断发生的错误是game.js:316 Uncaught TypeError: Cannot set property '-1' of undefined

I have been going at this for a while now.我已经有一段时间了。 I know it's probably just some simple logic error but for some reason, I just cannot pinpoint it.我知道这可能只是一些简单的逻辑错误,但由于某种原因,我无法确定它。 Can anyone find what I'm doing wrong?谁能发现我做错了什么?

Using .slice allows you to pull out bits of arrays.使用.slice可以提取 arrays 的位。 First pull out the rows at Y -> Y + H. Next, using map() to go through each of those to slice each into the columns from X -> X + W.首先拉出 Y -> Y + H 处的行。接下来,使用 map() 到 go 穿过每个行,将每个行切成 X -> X + W 的列。

You'll need to add safe guards to avoid exceeding the size or shape of the arrays.您需要添加安全防护装置以避免超出 arrays 的尺寸或形状。

 var arr = [ ['0', '1', 'd', 'a', '1', '1'], ['0', 's', '0', 'f', 's', 'g'], ['b', 'x', 'e', '0', 'v', 'a'], ['a', 'e', 'n', '0', 'z', 'm'], ['b', 'x', 'e', '0', 'v', 'a'], ]; console.log(snapshot(2, 1, 4, 2)); function snapshot(x, y, w, h) { return arr.slice(y, y + h).map(a => a.slice(x, x + w)) }

This task can be simplified greatly by breaking it up into two steps:通过将其分为两个步骤,可以大大简化此任务:

Step 1: extract the desired complete rows第 1 步:提取所需的完整行

Step 2: extract the desired columns from the extracted rows.第 2 步:从提取的行中提取所需的列。

 const arr = [ ['0', '1', 'd', 'a', '1', '1'], ['0', 's', '0', 'f', 's', 'g'], ['b', 'x', 'e', '0', 'v', 'a'], ['a', 'e', 'n', '0', 'z', 'm'], ['b', 'x', 'e', '0', 'v', 'a'], ]; function snapshot (array, colStart, rowStart, cols, rows) { // slice out the rows const fullRows = array.slice(rowStart, rowStart+rows); console.log('fullRows:', fullRows); // cut down the rows const cutRows = fullRows.map(row => row.slice(colStart, colStart+cols)); return cutRows; } const result = snapshot(arr, 2, 1, 4, 2); console.log('result:', result);

First filter out unwanted rows and use map and slice首先filter掉不需要的行并使用mapslice

 const snapshot = (arr, x, y, w, h) => arr.filter((_, i) => i >= y && i < y + h).map((items) => items.slice(x, x + w)); var arr = [ ["0", "1", "d", "a", "1", "1"], ["0", "s", "0", "f", "s", "g"], ["b", "x", "e", "0", "v", "a"], ["a", "e", "n", "0", "z", "m"], ["b", "x", "e", "0", "v", "a"], ]; console.log(snapshot(arr, 2, 1, 4, 2));

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

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