我有这个JSON
对象 myFilters :
{"filters":
{"role":"","jobs":[]}
}
我可以使用函数clean(myFilters
)从其中正确删除空对象:
function clean(obj) {
for (var propName in obj) {
if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === ""
) {
delete obj[propName];
}
}
现在,我的 myFilters 对象变为:
{"filters":
{ "jobs":[] }
}
现在如何从JSON
对象中删除空数组和键?
答案 0 :(得分:2)
您应该再添加一个条件,例如
function clean(obj) {
for (var propName in obj) {
if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === "" ||
Array.isArray(obj[propName]) && obj[propName].length === 0
) {
delete obj[propName];
}
}
}
答案 1 :(得分:1)
您应该先检查属性的类型,然后再通过 typeof
职位属性是一个对象,您可以通过其属性检查其值 长度。如果其 length 等于0,则为空。
function clean(obj) {
for (var propName in obj) {
if (typeof (obj[propName]) == 'object') {
if (obj[propName].length == 0) {
delete obj[propName];
}
} else {
if (
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === ""
) {
delete obj[propName];
}
}
}
}
答案 2 :(得分:1)
我喜欢Saveli Tomac's solution,所以我赞成。让我也向您展示原始解决方案的其他缩写。
如前所述,如果要查找空数组,则需要再检查2件事。那么,如何更轻松地检查null
,undefined
和''
值呢?
if (!undefined) { console.log('undefined needs to be deleted') };
if (!null) { console.log('null needs to be deleted') };
if (!'') { console.log(`'' needs to be deleted`) };
检查Array.length
是否具有0
值也可以缩短,如下所示:
const array1 = [];
const array2 = [1,2,3];
if (!array1.length) { console.log('array1 has 0 length') };
if (!array2.length) { console.log('array2 has 0 length') };
因此,根据这些代码段,您可以像以下内容一样进行其他缩短:
// extended with other types for the demo
let myObject = { "filters": { "role": "", "jobs": [], "nullValue": null, "undefinedIsHere": undefined, "arrayWithValue": [1,2,3], "stringValue": "hello", "numberishere": 123 } };
const clean = (obj) => {
for (let propName in obj) {
if (
!obj[propName] ||
Array.isArray(obj[propName]) && !obj[propName].length
) { delete obj[propName] };
}
}
clean(myObject.filters);
console.log(myObject);
或使用1️⃣内衬:
// extended with other types for the demo
let myObject = { "filters": { "role": "", "jobs": [], "nullValue": null, "undefinedIsHere": undefined, "arrayWithValue": [1,2,3], "stringValue": "hello", "numberishere": 123 } };
const clean = (obj) => {
Object.keys(obj).forEach(propName => (!obj[propName] || Array.isArray(obj[propName]) && !obj[propName].length) && delete obj[propName]);
}
clean(myObject.filters);
console.log(myObject);
在此处进一步阅读:
我希望这会有所帮助!
答案 3 :(得分:0)
应该是这样的:
function clean(obj) {
for (var propName in obj) {
if (obj.hasOwnProperty(propName) &&
obj[propName] === null ||
obj[propName] === undefined ||
obj[propName] === "" ||
(Array.isArray(obj[propName]) && obj[propName].length <= 0)
) {
delete obj[propName];
}
}
}
答案 4 :(得分:0)
Saveli的答案应该可以正常工作。这是您可以用来获得相同结果的另一种方法。
const object = {
"filters": {
"role": "",
"jobs": [],
"foo": undefined,
"baz": null,
"bar": {},
"moreJobs": ['1', '2']
}
}
const result = {
filters: Object.keys(object.filters).reduce((acc, key) => {
if (
object.filters[key] !== null &&
object.filters[key] !== undefined &&
object.filters[key] !== '' &&
typeof object.filters[key] === 'object' && Object.keys(object.filters[key]).length > 0
) {
acc[key] = object.filters[key];
}
return acc;
}, {})
};
console.log(result);
答案 5 :(得分:0)
尝试一下:
var filterObj = {
"filters": {
"role": "",
"jobs": []
}
};
for (var i in filterObj) {
for (var j in filterObj[i]) {
if ((filterObj[i][j] === null) ||
(filterObj[i][j] === undefined) ||
(filterObj[i][j].length === 0)) {
delete filterObj[i][j];
}
}
}
console.log(filterObj);