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
Post a Comment