I have a huge Javascript array (20MB).
The following code doesn’t work, I need to recursively search the big array, and delete any entrie where the array key matching an item in the removal list.
let largeArray = //Call's to API.
smallArray = clean(largeArray);
let removal = ["geocoded_waypoints", "request", "routes"];
console.log("Routes: " + smallArray);
function clean(obj) {
// For each item in the multidomensional array.
Object.keys(obj).forEach(key => {
// For each item in the removal array. Check it if needs to be removed.
let removeThis = false;
removal.every((element) => {
if (key === element) {
removeThis = true;
}
});
// Check if the array is a value, or an array. loop recursively as required.
if (removeThis === false) {
if (typeof obj[key] === 'object') {
// Item needs to be kept, if it's an array, recurse.
obj[key] = clean(obj[key]);
}
} else {
console.log("Removing: Key: " + key + ", Value: " + obj[key]);
delete obj[key];
}
});
return obj;
}