I have this: [{id: '1', name: 'name1'},{id: '2', name: 'name2'}...]
I would like to convert the id
field to an integer instead of a string. What’s the most elegant way of doing it using loash?
I have this: [{id: '1', name: 'name1'},{id: '2', name: 'name2'}...]
I would like to convert the id
field to an integer instead of a string. What’s the most elegant way of doing it using loash?
Loop over the array with _.forEach()
and convert the ID to an integer with parseInt()
.
_.forEach(yourArray, function(obj) {
obj.id = parseInt(obj.id, 10);
});
I know this isn't lodash but you can do this easily in vanilla ES6:
const newArr = oldArr.map(({id, name}) => ({id: Number(id), name});