I have a object
var obj = {
x: 'abc',
y: '6',
z: '8'
};
I want to convert y
and z
to int
type. There are 2 ways:
- Using
.mapObject
, I choose the way because when I want more keys, just changekeys
variable, no more lines
var keys = ['y', 'z'];
_.mapObject(obj, function(value, key) {
return _.contains(keys, key) ? parseInt(value) : value;
});
- Using
.tap
, just reference, If I want more keys, more lines, more code
_.tap(obj, function(obj){
obj.y = parseInt(obj.y);
obj.z = parseInt(obj.z);
});