从javascript对象删除冗余属性
问题说明
我想从对象中删除所有空属性(包括空字符串数组)。
I'd like to remove all empty properties (including arrays of empty strings) from an object.
{ "someString" : "some text", "someObject" : { "array" : [ "", "" ] } }
例如我想从这个示例对象中删除整个分支someObject。
E.g. I'd like to remove the entire branch "someObject" from this example object.
我应该如何使用javascript进行此操作?
How should I go about doing this with javascript?
我可以编写一个递归迭代每个属性并删除空属性的函数,但在上面的示例对象中,不会删除包含空字符串的数组。
I can write a function that iterates of over each property recursively and removes empty ones, but in the example object above the array containing empty strings would not be removed.
我应该清楚,我正在寻找一个递归函数来处理比例子更复杂的对象。任意组合中的多个级别的字符串,对象和数组。
I should be clear, I am looking to write a recursive function that will handle far more complex objects than the example. Multiple levels of strings, objects and arrays in any combination.
问题是递归函数从顶部开始并向下穿过对象。因此,在一个非常长(并且是多余的)分支的末尾的空字符串将被删除,但分支本身不会。
The problem is that a recursive function starts at the top and works its way down through the object. So an empty string at the end of a very long (and otherwise redundant) branch, will be deleted but the branch itself wont.
正确答案
到达底部后,只需如下所示遍历到根:
After reaching bottom, just traverse back to the root like so:
(function () {
Object.removeEmptiness = removeEmptiness;
function isArray(obj) {
return obj && Object.prototype.toString.call(obj) === "[object Array]" || false;
}
function isObject(obj) {
return obj && typeof obj == "object" || false;
}
function removeEmptiness(root, undef) {
var removeProps;
removeProps = function (obj, key, parent) {
var i, isFullyEmpty = true,
value;
if (isArray(obj)) {
//.length not cached on purpose
for (i = 0; i < obj.length; i) {
value = obj[i];
if (isObject(value)) {
removeProps(value, i, obj);
isFullyEmpty = false;
} else if (value === "" || value === undef) {
obj.splice(i--, 1);
} else {
isFullyEmpty = false;
}
}
} else {
for (i in obj) {
value = obj[i];
if (isObject(value)) {
removeProps(value, i, obj);
isFullyEmpty = false;
} else if (value === "" || value === undef) {
delete obj[i];
} else {
isFullyEmpty = false;
}
}
}
if (key !== undef && isFullyEmpty) {
delete parent[key];
removeProps(root);
}
};
removeProps(root);
return root;
}
})();
测试套房:
var obj = { "someString" : "some text", "someObject2" : {"k":"", "v": ["k"] }, "someArray3": [{}, ["","",""] ], "someObject" : { "array" : [ "", "", {"hello": ["",""] } ] } };
Object.removeEmptiness( obj );
console.log( JSON.stringify( obj ) );
//"{"someString":"some text","someObject2":{"v":["k"]}}"
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfjkek
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20