gpt4 book ai didi

javascript - 修改一个清理JSON对象的javascript函数

转载 作者:行者123 更新时间:2023-12-03 03:05:37 25 4
gpt4 key购买 nike

所以,这是我的函数,它从 Javascript 中的 JSON 对象中删除空和 null 属性。我需要该函数来删除空嵌套对象,但它现在没有执行此操作。我曾多次尝试修改此功能,但都失败了(我是从本网站的旧帖子中得到的)。

有人可以帮我解决这个问题吗?

函数

function clean_object(test, recurse) {
for (var i in test) {
if (test[i] === null || test[i] == "" ) {
delete test[i];
} else if (recurse && typeof test[i] === 'object' ) {
clean_object(test[i], recurse);
}
}
}

清理前的对象

{ "data.openstack.public_ipv4": "falseip" }

清洁后的物体

{"data":{"openstack":{}}}

我需要什么

{}

提前致谢!

最佳答案

通过作为部分方法提供的第一个开发步骤,我只是想让示例以清除所有对象 null 值和零长度字符串的方式工作 -值(value)观...

function clearEmptyValuesRecursively(obj) {
if (Array.isArray(obj)) {
obj.forEach(function (item) {

clearEmptyValuesRecursively(item);
});
} else {
Object.keys(obj).forEach(function (key) {
var value = obj[key];

if ((value === null) || (value === '')) {

delete obj[key];

} else if (typeof value !== 'string') {

clearEmptyValuesRecursively(value);
}
});
}
return obj;
}


var data = {
"data": {
"openstack": {},
"fullstack": "fullstack",
"emptyString": ""
},
"emptyData": null
};

console.log('data - before : ', JSON.stringify(data));
clearEmptyValuesRecursively(data);
console.log('data - after : ', JSON.stringify(data));

data = {
"data": {
"openstack": {}
}
};

console.log('data - before : ', JSON.stringify(data));
clearEmptyValuesRecursively(data);
console.log('data - after : ', JSON.stringify(data));
.as-console-wrapper { max-height: 100%!important; top: 0; }

...在第二步中我回收了上述方法。这次构建递归工作函数主要是为了清除空的主(数据)结构,例如 {}[],但它也负责删除空值,如已经所示用第一种方法。总而言之,这也是OP所要求的......

function clearEmptyStructuresRecursively(obj) {
function isEmptyStructure(type) {
return ((Object.keys(type).length === 0) || (Array.isArray(type) && (type.length === 0)));
}
function isEmptyValue(type) {
return ((type == null) || (type === '')); // undefined or null or zero length string value.
}
if (Array.isArray(obj)) {
obj.forEach(function (item) {

clearEmptyStructuresRecursively(item);
});
} else if (obj && (typeof obj !== 'string')) {

Object.keys(obj).forEach(function (key) {
var value = obj[key];

if (isEmptyValue(value) || isEmptyStructure(value)) {
delete obj[key]; // ... delete ... and step into recursion ...

clearEmptyStructuresRecursively(obj);
}
clearEmptyStructuresRecursively(value);
});

Object.keys(obj).forEach(function (key) {
var value = obj[key];

if (isEmptyValue(value) || isEmptyStructure(value)) {
delete obj[key]; // ... final delete.
}
});
}
return obj;
}

var data = {
"data": {
"openstack": {}
}
};

console.log('data - before : ', JSON.stringify(data));
clearEmptyStructuresRecursively(data);
console.log('data - after : ', JSON.stringify(data));


data = {
"data": {
"openstack": {},
"fullstack": "fullstack",
"emptyString": ""
},
"emptyData": null
};

console.log('data - before : ', JSON.stringify(data));
clearEmptyStructuresRecursively(data);
console.log('data - after : ', JSON.stringify(data));
.as-console-wrapper { max-height: 100%!important; top: 0; }

关于javascript - 修改一个清理JSON对象的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47182483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com