Skip to content

Instantly share code, notes, and snippets.

@Gift-Stack
Created September 21, 2021 22:38
Show Gist options
  • Save Gift-Stack/b5732142ee32de14cedcac52fd1c4757 to your computer and use it in GitHub Desktop.
Save Gift-Stack/b5732142ee32de14cedcac52fd1c4757 to your computer and use it in GitHub Desktop.
function getUniqueSortedNumbers(tree) {
let uniqueSortedNumbers = []
;(function sortTree(tree) {
const ObjArr = Object.values(tree)
for (let i = 0; i < ObjArr.length; i++) {
// Assuming the values are objects and numbers only
if (typeof ObjArr[i] === 'object') {
sortTree(ObjArr[i])
} else uniqueSortedNumbers.push(ObjArr[i])
}
const set = new Set(uniqueSortedNumbers)
const uniqueArr = Array.from(set)
uniqueSortedNumbers = uniqueArr.sort((a, b) => a - b)
})(tree)
console.log(uniqueSortedNumbers)
return uniqueSortedNumbers
}
getUniqueSortedNumbers({
a: {
x: 3,
y: {
d: 2,
e: 2,
f: {
g: 4
},
z: 8
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment