digout
Version:
Digout recursively extracts and navigates deeply nested data structures, bridging the gaps left by JavaScript's native methods. Flatten multidimensional arrays into a single linear vertical, simplifying preprocessing tasks in AI and machine learning.
78 lines (44 loc) • 3.55 kB
JavaScript
const digout = (input, mode = 'BOTH') => { //Define a function that takes an input and a mode enum which defaults to 'BOTH'...
let result = mode === 'BOTH' ? {} : [] //...initialize the result data type as an object if the mode is 'BOTH', otherwise initialize it as an array...
const digoutRecursive = (input, parentKey = '') => { //...define a recursive function that takes an input and a parent key, the latter which defaults to an empty string...
if (Array.isArray(input)) { //...if the input is an array...
input.forEach((item, index) => { //...for each item in the array...
const newKey = parentKey ? `${parentKey}.[${index}]` : `[${index}]` //...create a new key based on the parent key and the index...
if (typeof item === 'object' && item !== null) { //...if the item is an object and not null...
digoutRecursive(item, newKey) //...recursively call the function with the item and the new key...
} else { //...otherwise...
if (mode === 'KEYS') { //...if the mode enum is 'KEYS'...
result.push(newKey) //...push the newKey to the result...
} else if (mode === 'VALUES') { //...if the mode enum is 'VALUES'...
result.push(item) //...push the item to the result...
} else if (mode === 'BOTH') { //...if the mode enum is 'BOTH'...
result[newKey] = item //...set the newKey as the key and the item as the value in the result...
}
}
})
} else if (typeof input === 'object' && input !== null) { //...otherwise if the input is an object and not null...
for (let key in input) { //...for each key in the input...
if (input.hasOwnProperty(key)) { //...if the input has the key...
const newKey = parentKey ? `${parentKey}.${key}` : key //...create a new key based on the parentKey and the key...
if (typeof input[key] === 'object' && input[key] !== null) { //...if the input at the key is an object and not null...
digoutRecursive(input[key], newKey) //...recursively call the function with the input at the key and the newKey...
} else { //...otherwise...
if (mode === 'KEYS') { //...if the mode enum is 'KEYS'...
result.push(newKey) //...push the newKey to the result...
} else if (mode === 'VALUES') { //...if the mode enum is 'VALUES'...
result.push(input[key]) //...push the input at the key to the result...
} else if (mode === 'BOTH') { //...if the mode enum is 'BOTH'...
result[newKey] = input[key] //...set the newKey as the key and the input at the key as the value in the result...
}
}
}
}
}
}
digoutRecursive(input) //...call the recursive function with the input...
return result //...return the result...
}
Object.digout = function(obj, mode = 'BOTH') { //...extend the Object constructor with a digout method...
return digout(obj, mode) //...which appropriately calls the digout function with the object and the mode...
}
module.exports = digout //...and export digout as a module which can be called as a method on an object or as a standalone function