从Javascript中的Object中删除空白属性

从Javascript中的Object中删除空白属性

如何删除这些都是属性undefinednull在JavaScript对象?

(问题类似于这个阵列)


MMTTMM
浏览 1507回答 3
3回答

喵喵时光机

你可以遍历对象:var test = {&nbsp; &nbsp; test1 : null,&nbsp; &nbsp; test2 : 'somestring',&nbsp; &nbsp; test3 : 3,}function clean(obj) {&nbsp; for (var propName in obj) {&nbsp;&nbsp; &nbsp; if (obj[propName] === null || obj[propName] === undefined) {&nbsp; &nbsp; &nbsp; delete obj[propName];&nbsp; &nbsp; }&nbsp; }}clean(test);如果您担心此属性删除没有运行对象的proptype链,您还可以:function clean(obj) {&nbsp; var propNames = Object.getOwnPropertyNames(obj);&nbsp; for (var i = 0; i < propNames.length; i++) {&nbsp; &nbsp; var propName = propNames[i];&nbsp; &nbsp; if (obj[propName] === null || obj[propName] === undefined) {&nbsp; &nbsp; &nbsp; delete obj[propName];&nbsp; &nbsp; }&nbsp; }}关于null vs undefined的一些注释:test.test1 === null; // truetest.test1 == null; // truetest.notaprop === null; // falsetest.notaprop == null; // truetest.notaprop === undefined; // truetest.notaprop == undefined; // true

茅侃侃

使用一些ES6 / ES2015:1)一个简单的单行内容删除内联项目而不分配:Object.keys(myObj).forEach((key)&nbsp;=>&nbsp;(myObj[key]&nbsp;==&nbsp;null)&nbsp;&&&nbsp;delete&nbsp;myObj[key]);jsbin2)这个例子被删除了......3)作为函数编写的第一个示例:const&nbsp;removeEmpty&nbsp;=&nbsp;obj&nbsp;=>&nbsp;{ &nbsp;&nbsp;Object.keys(obj).forEach(key&nbsp;=>&nbsp;obj[key]&nbsp;==&nbsp;null&nbsp;&&&nbsp;delete&nbsp;obj[key]);};jsbin4)此函数也使用递归来从嵌套对象中删除项目:const&nbsp;removeEmpty&nbsp;=&nbsp;obj&nbsp;=>&nbsp;{ &nbsp;&nbsp;Object.keys(obj).forEach(key&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(obj[key]&nbsp;&&&nbsp;typeof&nbsp;obj[key]&nbsp;===&nbsp;"object")&nbsp;removeEmpty(obj[key]);&nbsp;//&nbsp;recurse &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(obj[key]&nbsp;==&nbsp;null)&nbsp;delete&nbsp;obj[key];&nbsp;//&nbsp;delete &nbsp;&nbsp;});};jsbin4b)这类似于4),但它不是直接改变源对象,而是返回一个新对象。const&nbsp;removeEmpty&nbsp;=&nbsp;obj&nbsp;=>&nbsp;{ &nbsp;&nbsp;const&nbsp;newObj&nbsp;=&nbsp;{}; &nbsp;&nbsp;Object.keys(obj).forEach(key&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(obj[key]&nbsp;&&&nbsp;typeof&nbsp;obj[key]&nbsp;===&nbsp;"object")&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newObj[key]&nbsp;=&nbsp;removeEmpty(obj[key]);&nbsp;//&nbsp;recurse &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(obj[key]&nbsp;!=&nbsp;null)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newObj[key]&nbsp;=&nbsp;obj[key];&nbsp;//&nbsp;copy&nbsp;value &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); &nbsp;&nbsp;return&nbsp;newObj;};5)基于@ MichaelJ.Zoidl的回答使用和的4b)的功能方法。这个也返回一个新对象:filter()reduce()const&nbsp;removeEmpty&nbsp;=&nbsp;obj&nbsp;=> &nbsp;&nbsp;Object.keys(obj) &nbsp;&nbsp;&nbsp;&nbsp;.filter(k&nbsp;=>&nbsp;obj[k]&nbsp;!=&nbsp;null)&nbsp;//&nbsp;Remove&nbsp;undef.&nbsp;and&nbsp;null. &nbsp;&nbsp;&nbsp;&nbsp;.reduce( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(newObj,&nbsp;k)&nbsp;=> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;typeof&nbsp;obj[k]&nbsp;===&nbsp;"object" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;?&nbsp;{&nbsp;...newObj,&nbsp;[k]:&nbsp;removeEmpty(obj[k])&nbsp;}&nbsp;//&nbsp;Recurse. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;{&nbsp;...newObj,&nbsp;[k]:&nbsp;obj[k]&nbsp;},&nbsp;//&nbsp;Copy&nbsp;value. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{} &nbsp;&nbsp;&nbsp;&nbsp;);jsbin6)与4)相同,但使用ES7 / 2016&nbsp;Object.entries()。const&nbsp;removeEmpty&nbsp;=&nbsp;(obj)&nbsp;=>&nbsp; &nbsp;&nbsp;Object.entries(obj).forEach(([key,&nbsp;val])&nbsp;=>&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(val&nbsp;&&&nbsp;typeof&nbsp;val&nbsp;===&nbsp;'object')&nbsp;removeEmpty(val) &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(val&nbsp;==&nbsp;null)&nbsp;delete&nbsp;obj[key]})5b)&nbsp;使用递归并使用ES2019返回新对象的另一个功能版本&nbsp;:&nbsp;Object.fromEntries()const&nbsp;removeEmpty&nbsp;=&nbsp;obj&nbsp;=> &nbsp;&nbsp;Object.fromEntries( &nbsp;&nbsp;&nbsp;&nbsp;Object.entries(obj) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(([k,&nbsp;v])&nbsp;=>&nbsp;v&nbsp;!=&nbsp;null) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(([k,&nbsp;v])&nbsp;=>&nbsp;(typeof&nbsp;v&nbsp;===&nbsp;"object"&nbsp;?&nbsp;[k,&nbsp;removeEmpty(v)]&nbsp;:&nbsp;[k,&nbsp;v])) &nbsp;&nbsp;);7)与4)相同,但在简单的ES5中:function&nbsp;removeEmpty(obj)&nbsp;{ &nbsp;&nbsp;Object.keys(obj).forEach(function(key)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(obj[key]&nbsp;&&&nbsp;typeof&nbsp;obj[key]&nbsp;===&nbsp;'object')&nbsp;removeEmpty(obj[key]) &nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;if&nbsp;(obj[key]&nbsp;==&nbsp;null)&nbsp;delete&nbsp;obj[key] &nbsp;&nbsp;});};jsbin

手掌心

如果您使用的是lodash或underscore.js,这是一个简单的解决方案:var&nbsp;obj&nbsp;=&nbsp;{name:&nbsp;'John',&nbsp;age:&nbsp;null};var&nbsp;compacted&nbsp;=&nbsp;_.pickBy(obj);这只适用于lodash 4,pre lodash 4或underscore.js,使用_.pick(obj, _.identity);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript