Last Commit: 2024-01-06 17:50:27

views:

Table of Content

JS Array

Flatten

The problem is from the link.

There is a multi-level layers array:

var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];

How can I get a plain array and sort it without repetition? My thinking is as below:

function flatten(source){
    if (Object.prototype.toString.call(source) !== '[object Array]'){
        return [source];
    }
    let target = [];
    for (let i in source){
        target = target.concat(flatten(source[i]));
    }
    return target;
}
let result = Array.from(new Set(flatten(arr))).sort((a,b)=>a-b);
console.log(result);

There is a more clear way I found:

Array.from(new Set(arr.flat(Number.POSITIVE_INFINITY))).sort((a,b)=>a-b);

If you have some better way to solve this question, welcome to write it!

Combine

  • [1, 2, 3, 4, 6, 7, 9, 13, 15] => ['1->4', '6->7', '9', '13', '15']
function combine(source) {
    const ret = [];
    let start = null;
    let index = 0;
    while (index < source.length) {
        if (start == null) start = index;
        if (source[index] + 1 !== source[index + 1]) {
            ret.push(start === index ? `${source[index]}` : `${source[start]}->${source[index]}`);
            start = null;
        }
        index++;
    }
    return ret;
}