function removeKeys(obj, keys) {
if (Array.isArray(obj)) return obj.map((item) => removeKeys(item, keys));
if (typeof obj === "object" && obj !== null) {
return Object.keys(obj).reduce((previousValue, key) => {
return keys.includes(key)
? previousValue
: { ...previousValue, [key]: removeKeys(obj[key], keys) };
}, {});
}
return obj;
}
const data = {
details: [
{
userId: "user01",
documents: [
{
document: {
id: "doc_pp_01",
type: "pp",
number: "222333444",
personName: {
first: "JAMES",
middle: "JOHNIE",
last: "SMITH"
},
nationality: "AL",
dateOfBirth: "1990-01-01",
issuingCountry: "AL",
expiryDate: "2025-01-01",
gender: "MALE"
}
}
]
}
],
criteria: {
"id:": "AB1234",
fullName: "James Johnie Smith"
}
};
console.log(
"Without `fullName` and `details`",
removeKeys(data, ["fullName", "details"])
);
console.log("Without `id:` and `gender`", removeKeys(data, ["id:", "gender"]));