Resize 2d array immutably

问题: Demo: https://jsfiddle.net/yxm9d4L0/2/ Given a 2D 3x3 array: let arr = [ [null, null, null], [null, {'hello': 'world'}, null], [null, null, null] ] and n...

问题:

Demo: https://jsfiddle.net/yxm9d4L0/2/

Given a 2D 3x3 array:

let arr = [
    [null, null, null],
    [null, {'hello': 'world'}, null],
    [null, null, null]
]

and new sizes greater than current length:

width = 10; height = 8;

or less than to current length:

width = 1; height = 2;

and a default fill value of null.

How would you resize this array immutably based on the new widths or heights?

function handleArrUpdate(h,w) {
  let currentArr = [
    [null, null, null],
    [null, {'hello': 'world'}, null],
    [null, null, null]
  ];
  let currentArrWidth = currentArr[0].length;
  let currentArrHeight = currentArr.length;
  let updatedArr = [...currentArr];

  if (h > currentArrHeight) {
    let arr = createArray(w, null);
    for (let i = 0; i <= h; i++) {
      updatedArr.concat(arr)
    }
  } else if (h < currentArrHeight) {
    let arr = createArray(w, null);
    for (let i = 0; i <= h; i++) {
      updatedArr.concat(arr)
    }
  }

  if (w > currentArrWidth) {
    let arr = createArray(w, null);
    for (let i = 0; i <= h; i++){
      updatedArr[i].concat(arr)
    }
  } else if (w < currentArrWidth) {
    let arr = createArray(w, null);
    for (let i = 0; i <= h; i++) {
      updatedArr[i].concat(arr)
    }
  }

  console.log('updated array', updatedArr);

  function createArray(size, defaultVal) {
    let arr = new Array(size);
    for (let i = 0; i < size; i++) {
      arr[i] = defaultVal;
    }
    return arr;
  }
}

handleArrUpdate(10,8);
handleArrUpdate(1,2);
handleArrUpdate(3,3);

Currently this code only works for concatenating higher values. I am getting Cannot read property 'concat' of undefined.

Desired Outputs:

//10x8
[
    [null, null, null, null, null, null, null, null, null, null],
    [null, { 'hello': 'world' }, null, null, null, null, null, null, null, null],
    [null, null, null, null, null, null, null, null, null, null],
    [null, null, null, null, null, null, null, null, null, null],
    [null, null, null, null, null, null, null, null, null, null],
    [null, null, null, null, null, null, null, null, null, null],
    [null, null, null, null, null, null, null, null, null, null],
    [null, null, null, null, null, null, null, null, null, null]
];

// 1x2
[
    [null, null]
]

// 3x3
[
    [null, null, null],
    [null, { 'hello': 'world' }, null],
    [null, null, null]
]
// 4x2
[
    [null, null],
    [null, { 'hello': 'world' }],
    [null, null],
    [null, null]
]

回答1:

Here is a way to do it using Array.from. However, it will copy existing content, so Objects references will be copied and will still point to the same objects.

const arr = [
  [null, null, null],
  [null, {'hello': 'world'}, null],
  [null, null, null]
];

function resize(arr, width, height, val = null) {
  const newRow = row => Array.from({ length: width }, (_, i) => {
    return i < row.length ? row[i] : val
  });
  return Array.from({ length: height }, (_, i) => {
    return i < arr.length ? newRow(arr[i]) : Array.from({ length: width }, () => val);
  });
}

console.log(JSON.stringify(resize(arr, 5, 5, 0)));
console.log(JSON.stringify(resize(arr, 2, 4, null)));


回答2:

You can use filter and push based on the width and height.

function handleArrUpdate(h,w) {
  let currentArr = [[null, null, null],[null, {'hello': 'world'}, null],[null, null, null]];
  let op = currentArr.map(e => {
   return  e.length > w ? e.filter( (_, index ) => index < w) : e.concat( new Array ( w - e.length ).fill( null ))
  })
  if(h > currentArr.length){
    let temp = h-currentArr.length
    while(temp--)op.push( new Array(w).fill(null) )
  } else {
    op = op.filter( ( _, index ) => index < h )
  }
  return op
}

//console.log(handleArrUpdate(10,8));
console.log(handleArrUpdate(1,2));
console.log(handleArrUpdate(3,3))

  • 发表于 2019-02-18 12:04
  • 阅读 ( 187 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除