How does JS convert an one-dimensional array into a two-dimensional array?

for example,
arr1 = [2 br, 2, 2, 5, 7];
split according to the same,
want to get
arr2 = [2, 2, 2, 5], [6, 6, 6, 6, 6];

Mar.16,2021

let arr1 = [2,2,2,5,5,6,6,6,6,7];
function trans (arr) {
    let pre = NaN
    let result = []
    let curArr
    arr.forEach(cur => {
        if (cur === pre) {
            curArr.push(cur)
        } else {
            curArr = [cur]
            result.push(curArr)
            pre = cur
        }
    })
    return result
}
trans(arr1)

[2,2,2,5,5,6,6,6,6,7]
.reduce((m,n) => (m[n] = (m[n] || []).concat(n), m),[])
.filter(a => a)

clipboard.png


is your one-dimensional array sorted?
if it is already arranged, it is relatively simple to handle, otherwise it will be more troublesome.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e67407-45c1b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e67407-45c1b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?