javascript - How do I filter out a key from an object? -


i have following object in js. how can select keys except first financial_year , put new empty object?

i understand can obj["mainline_revenue"] select individual elements long list , don't want type elements keys individually.

var obj = {financial_year: 1, mainline_revenue: 18743, regional_revenue: 2914, other_revenue: 3198, salaries_wages: -6897}  var newobj = {} 

new object this:

console.log(newobj) {mainline_revenue: 18743, regional_revenue: 2914, other_revenue: 3198, salaries_wages: -6897} 

you clone object object.assign use delete removed undesired property:

var newobj = object.assign({}, obj); delete newobj.financial_year; 

of course there other more functional ways achieve this, maybe filtering keys, reducing object:

var newobj = object.keys(obj).filter(key =>    key !== 'financial_year' ).reduce((newobj, currkey) =>   (newobj[currkey] = obj[currkey], newobj),  {}); 

though approach more suited if had an array of keys wanted filter out, , check if key in array in filter callback.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -