arraymania
Version:
It make all the nested array into one dimension array and more functionalities will come soon
37 lines (33 loc) • 978 B
JavaScript
const {
getSortNestedArray,
getSortedObjectKeys,
} = require("./helper/getThingsSorted");
function deepSorting(arr) {
try {
if (Array.isArray(arr)) {
let finalResp = arr.reduce((acc, val) => {
if (val === undefined || val === false || val === null) {
return acc;
}
/* In case of string */
if (typeof val === "string" || typeof val === "number") {
return acc.concat(val);
} else if (Array.isArray(val)) {
val = getSortNestedArray(val);
return [...acc, val];
} else if (typeof val === "object") {
val = getSortedObjectKeys(val);
return acc.concat(val);
}
}, []);
return finalResp.sort();
} else if (typeof arr === "object") {
return getSortedObjectKeys(arr);
} else {
throw Error("You should provide array or object to deepSorting");
}
} catch (err) {
throw err;
}
}
module.exports = { deepSorting };