Reputation: 1185
I have a javascript object like the one below with several fields and arrays and arrays within arrays. I'm only interested only in non-junk fields and wanted to remove all the junk properties from the objects. How can I do that using lodash?
I can do that by using delete
, but I don't want to do that because that list is huge and I don't want to keep changing my code to write a delete every time a new junk property is added to my object.
{
state: 'New York',
country: 'usa',
counties : [['washington','DC'],'Long Island',['New','Jersey']],
city : 'Manhattan',
zip: '43543545',
JunkOne : ['3453454','45345','45345'],
JunkTwo: '5454545',
JunkThree: {adc:'4545',vdfd:'45345'}
}
Upvotes: 7
Views: 33330
Reputation: 29
Better to use map with following function to extract only specific keys which you need
function extractKeyValuesFromObject(_obj, _keys) {
let result = {};
_keys.forEach(_k => result[_k] = _obj[_k]);
return result;
}
Upvotes: 0
Reputation: 2569
Disappointed that none of these answers solve the problem without making a new object (copy). I want something that works like _.set()
that can delete at a path without causing an error when the path doesn't completely exist. Came up with this:
_.set(obj, 'path.to.property', null);
delete obj.path.to.property;
Upvotes: 0
Reputation: 1795
If you need some flexiblity in what to pick, lodash _.pickBy
is really nice.
For example:
const result = _.pickBy(someObject, function(value, key) {
return key.search(/Junk/) == -1
});
If the function returns truthy, the key/value pair is picked from the object.
Upvotes: 0
Reputation: 1
You can use Object.keys()
, RegExp
/^Junk/
to check each property of object, call delete
if match is found
var obj = {
state: 'New York',
country: 'usa',
counties : [['washington','DC'],'Long Island',['New','Jersey']],
city : 'Manhattan',
zip: '43543545',
JunkOne : ['3453454','45345','45345'],
JunkTwo: '5454545',
JunkThree: {adc:'4545',vdfd:'45345'}
};
var keys = Object.keys(obj);
var re = /^Junk/;
for (var i = 0; i < keys.length; i++) {
if (re.test(keys[i])) {
delete obj[keys[i]]
}
}
console.log(obj)
Upvotes: 0
Reputation: 391
You can use lodash.pick()
to create an object with only the requested keys. For example:
var city = {
state: 'New York',
country: 'usa',
counties : [['washington','DC'],'Long Island',['New','Jersey']],
city : 'Manhattan',
zip: '43543545',
JunkOne : ['3453454','45345','45345'],
JunkTwo: '5454545',
JunkThree: {adc:'4545',vdfd:'45345'}
}
var lodash = require('lodash');
city = lodash.pick(city, ['state', 'country', 'counties','city','zip']);
City now should have all the useful data, and none of the junk.
Upvotes: 10
Reputation: 4329
use either delete operator to delete specific properties
delete data.JunkOne;
or use object.assign to select specific properties
var a = Object.assign({}, { counties: data.counties});
EDIT:
Doing it lodash way would be
var a = _.omit(data, ['JunkOne']);
or
var a = _.pick(data, ['counties']);
Upvotes: 18
Reputation: 115940
You can use _.pick
to create a new object with only a specific set of properties from your original object:
var objectWithNoJunk = _.pick(myObject, ['state', 'country', 'counties', 'city', 'zip']);
Upvotes: 7
Reputation: 21926
@aspillers answer is the same as mine but waaay shorter/better. Go with that.
var listOfGoodProps = [
"city",
"county",
...
];
Object.keys(yourObj).reduce(function(acc, key) {
if (listOfGoodProps.indexOf(key) !== -1)
acc[key] = yourObj[key];
return acc;
}, {});
No lodash needed, straight up ES 5.
Upvotes: 2