English 中文(简体)
是否有一个很好的方法将法尔西值从标本天体(不是阵列)中除去?
原标题:Is there a nice way in javascript to removing Falsy values from a javascript object (not an array)?
In JavaScript you have the nice .filter method to remove null or falsy values from arrays. So far I haven t been able to find a method to remove the same from JavaScript Objects. Why would this be? Currently you can create a function for arrays like : function stripNulls(arr) { return arr.filter(Boolean); } Is there a similar function that can be created for JS Objects, or is the way filter works not practical on JS Objects.
最佳回答
The answer to "can I do x to an object" (or an array for that matter) is usually "yes" and it frequently involves some form of reduce. If you want to filter falsy values you could do something like this: function filterFalsy(obj) { return Object.keys(obj).reduce((acc, key) => { if (obj[key]) { acc[key] = obj[key] } return acc }, {}) } const testObj = { a: test , b: 321, c: false } console.log(filterFalsy(testObj)) This returns a new object without falsy values and leaves the existing object alone.
问题回答
WARNING: There are better answers provided here. Also, thanks to comments made below user s should be warned using delete may provide suboptimal performance. Filtering invalid values is a little more complex in objects. At face value this will do what you want: var arr = [ apple , 43, false ]; var trueArr = arr.filter(Boolean); console.log(trueArr); var obj = { title : apple , id : 43, isOrange : false, test : asd }; Object.keys(obj) .filter(key => !obj[key]) .forEach(key => delete obj[key]); console.log(obj); However, this will not iterate over child objects / functions. This logic also directly modifies the original object (which may or may not be desired). That can easily changed by adding this logic to a function like so: function removeFalseyProperties(obj) { Object.keys(obj) .filter(key => !obj[key]) .forEach(key => delete obj[key]); return obj; } var testObj = { title : apple , id : 43, isOrange : false, test : asd }; var trutheyObj = removeFalseyProperties(testObj); console.log(trutheyObj);
If you have lodash installed in your project, you can simply use import _ from lodash ; _.pickBy(obj, Boolean)
falsy values are 0, undefined, null, false, etc. myArray .map(item => { // ... }) // Get rid of bad values .filter(Boolean); By passing Boolean we can remove all the falsy values.




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签