var matrixReshape = function(nums, r, c) { let pR = nums.length, pC = nums[0].length; if (pR * pC != r * c) { return nums; } let newArr = [], resultArr = [], count = 0; for (var value of nums) { newArr.push(...value); } console.log(newArr) for (let i = 0; i < r; i++) { let currArr = []; for (let j = 0; j < c; j++) { currArr.push(newArr[count]); count ++; } resultArr.push(currArr); } return resultArr; }; let nums = [[1,2,3,4]]; console.log(matrixReshape(nums, 2, 2))