Reputation: 758
Hi I am having an object like this, I want to return the property values as undefined
or null
if not present in the object. Here is my object.
var build= { 'ts1': 1, 'ts2': '2', 'ts3': 3 };
I am using _.pick
function to pick the values it only pick values that are present in the object.
Here is my code:
_.pick(build, [ 'ts0' 'ts1', 'ts2','ts5','ts6','ts7']);
I am getting the result like this:
{ 'ts1': 1, 'ts2': '2'}
and the expected output is:
{ 'ts0':null, 'ts1': 1, 'ts2': '2','ts5':null,'ts6':null,'ts7':null}
Please can anybody help me on this.
Upvotes: 0
Views: 2199
Reputation: 1634
You can iterate through the desired values and build a new object. _.set
will add the value to the new object. _.get
will attempt to get the value from the existing object, if it does not exist then it will use null
as the default value.
var build = {
'ts1': 1,
'ts2': '2',
'ts3': 3
};
var pickValues = ['ts0', 'ts1', 'ts2', 'ts5', 'ts6', 'ts7'];
var result = {};
_.forEach(pickValues, function(key) {
_.set(result, key, _.get(build, key, null));
});
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Upvotes: 1
Reputation: 16779
_.pick
is designed to give you a subset of the properties of an object. It will not assign a property to null
that had never existed in the original object.
However, you mentioned that:
I want to return the property values as
undefined
ornull
if not present in the object
Luckily for you, JavaScript already does this by default (returning undefined
) for all property accesses where the property does not exist on the target object. So, you could simply use the code you have already and use regular bracket property access notation to have property values default to undefined
:
var build = {
'ts1': 1,
'ts2': '2',
'ts3': 3
}
var result = _.pick(build, [
'ts0', 'ts1', 'ts2', 'ts5', 'ts6', 'ts7'
])
console.log(result['ts0']) //=> undefined
console.log(result['ts1']) //=> 1
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Edit: If you really need the properties to exist on the object and default to null
, you can use the following method:
var build = {
'ts1': 1,
'ts2': '2',
'ts3': 3
}
var result = _.reduce([
'ts0', 'ts1', 'ts2', 'ts5', 'ts6', 'ts7'
], function (o, e) {
o[e] = e in build ? build[e] : null
return o
}, {})
console.log(result['ts0']) //=> undefined
console.log(result['ts1']) //=> 1
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Upvotes: 4
Reputation: 1
You can use for..of
loop to iterate array, assign properties to object, set value to value of build
at that property, or null
var build = { 'ts1': 1, 'ts2': '2', 'ts3': 3 };
let props = ['ts0', 'ts1', 'ts2','ts5','ts6','ts7'];
let res = {};
for (let prop of props) res[prop] = build[prop] || null;
console.log(res);
Upvotes: 1
Reputation: 3883
That's just not how _.pick
works. You could use _.assign
to accomplish what you are trying to do.
var build = {
'ts1': 1,
'ts2': '2',
'ts3': 3
};
let result = _.assign({
ts0: null,
ts1: null,
ts2: null,
ts3: null,
ts4: null,
ts5: null,
ts6: null,
ts7: null
},
build);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Upvotes: 0