如果你想在删除键后删除空数组或对象,下面的实现就可以了。
function cleanData(data, deletingKeys) {
function isEmpty(obj) {
if (obj === null) return true;
if (Array.isArray(obj))
return obj.length === 0;
if (typeof obj === "object")
return Object.keys(obj).length === 0;
}
function removeKeyFrom(aData) {
if (Array.isArray(aData)) {
const done = aData.reduce((accum, ele) => {
const done = removeKeyFrom(ele);
if (!isEmpty(done))
accum.push(done);
return accum;
}, []);
return done.length > 0 ? done : null;
}
if (typeof aData === "object" && aData !== null) {
const done = Object.keys(aData).reduce((accum, key) => {
if (!deletingKeys.includes(key)) {
const done = removeKeyFrom(aData[key]);
if (!isEmpty(done)) // required for empty object element
accum[key] = done;
}
return accum;
}, {});
return (Object.keys(done).length > 0) ? done : null;
}
return aData;
}
return removeKeyFrom(data);
}
假设您的数据和删除条件如下:
const data = {
"details2": [{
"userId": "user01",
// `documents` should be removed since the value is the array of an empty object
"documents": [{
"details": { // this should be removed
"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",
"shouldBeRemoved": { // this should be removed since the value will be the empty object
"details": {
"whatever": 1234
}
}
}
};
const deleteKeys = ["details", "fullName"];
调用cleanData(data, deleteKeys)会导致
{
"details2": [
{
"userId": "user01"
}
],
"criteria": {
"id:": "AB1234"
}
}
而不是
{
"details2": [
{
"userId": "user01",
"documents": [
{}
]
}
],
"criteria": {
"id:": "AB1234",
"shouldBeRemoved": {}
}
}
这是运行的完整代码。
function cleanData(data, deletingKeys) {
function isEmpty(obj) {
if (obj === null) return true;
if (Array.isArray(obj))
return obj.length === 0;
if (typeof obj === "object")
return Object.keys(obj).length === 0;
}
function removeKeyFrom(aData) {
if (Array.isArray(aData)) {
const done = aData.reduce((accum, ele) => {
const done = removeKeyFrom(ele);
if (!isEmpty(done))
accum.push(done);
return accum;
}, []);
return done.length > 0 ? done : null;
}
if (typeof aData === "object" && aData !== null) {
const done = Object.keys(aData).reduce((accum, key) => {
if (!deletingKeys.includes(key)) {
const done = removeKeyFrom(aData[key]);
if (!isEmpty(done)) // required for empty object element
accum[key] = done;
}
return accum;
}, {});
return (Object.keys(done).length > 0) ? done : null;
}
return aData;
}
return removeKeyFrom(data);
}
const data = {
"details2": [{
"userId": "user01",
// `documents` should be removed since the value is the array of empty object
"documents": [{
"details": { // this should be removed
"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",
"shouldBeRemoved": { // this should be removed since the value is the empty object.
"details": {
"whatever": 1234
}
}
}
};
const deleteKeys = ["details", "fullName"];
const print = obj => JSON.stringify(obj, null, 2);
console.log(print(cleanData(data, deleteKeys)));