A few JavaScript solutions
The intuitive way:\n\nvar matrixReshape = function(nums, h, w) {\n    const m = nums.length, n = nums[0].length;\n    if (m * n !== h * w) return nums;\n    con
The intuitive way:\n```\nvar matrixReshape = function(nums, h, w) {\n    const m = nums.length, n = nums[0].length;\n    if (m * n !== h * w) return nums;\n    const res = [];\n    for (let i = 0, r = 0; r < m; r++) {\n        for (let c = 0; c < n; c++, i++) {\n            let rr = Math.floor(i / w);\n            if (!res[rr]) res.push([]);\n            res[rr].push(nums[r][c]);\n        }\n    }\n    return res;\n};\n```\nOne loop:\n```\nvar matrixReshape = function(nums, h, w) {\n    const m = nums.length, n = nums[0].length;\n    if (m * n !== h * w) return nums;\n    const res = [];\n    for (let i = 0; i < m * n; i++) {\n        let r = Math.floor(i / w);\n        if (!res[r]) res.push([]);\n        res[r].push(nums[Math.floor(i / n)][i % n]);\n    }\n    return res;\n};\n```\nTwo-liner:\n```\nvar matrixReshape = function(nums, h, w) {\n    const all = nums.reduce((all, row) => [...all, ...row], []);\n    return all.length === h * w ? new Array(h).fill(0).map((row, r) => all.slice(r * w, r * w + w)) : nums;\n};\n```
['JavaScript']