arraymania
Version:
It make all the nested array into one dimension array and more functionalities will come soon
50 lines (41 loc) • 1.23 kB
JavaScript
// const getSortedObjectKeys = require('./getSortObject')
function getSortNestedArray(arr) {
let finalResp = arr.reduce((acc, val) => {
/* In case of string */
if (typeof val === "string" || typeof val === "number") {
return [...acc, val]; //acc.concat(val)
} else if (Array.isArray(val)) {
let bab = getSortNestedArray(val);
return [...acc, bab];
} else if (typeof val === "object") {
val = getSortedObjectKeys(val);
return [...acc, val]; //acc.concat(val)
}
}, []);
return finalResp.sort();
}
function getSortedObjectKeys(obj) {
let arr = Object.keys(obj);
arr.sort();
const sortObj = arr.reduce((acc, key) => {
let getVal = obj[key];
if (
typeof getVal === "string" ||
typeof getVal === "number" ||
getVal === undefined ||
getVal === false ||
getVal === null
) {
acc[key] = getVal;
return acc;
} else if (Array.isArray(getVal)) {
acc[key] = getSortNestedArray(getVal);
return acc;
} else if (typeof getVal === "object") {
acc[key] = getSortedObjectKeys(getVal);
return acc;
}
}, {});
return sortObj;
}
module.exports = { getSortNestedArray, getSortedObjectKeys };