Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  107] [ 7]  / answers: 1 / hits: 52334  / 8 Years ago, thu, june 16, 2016, 12:00:00

i have a arrray of objects where i wish to convert the data from medicine to type string. The only problem is instead of returning the array of objects is returing me the array of medicine.



Example
input:



data = [{medicine: 1234, info: blabla},{medicine: 9585, info: blabla},..]


desired output:



data = [{medicine: 1234, info: blabla},{medicine: 9585, info: blabla},..]


What im getting?
Array of medicine numbers.



Here is my code:



var dataMedicines = _.map(data, 'medicine').map(function(x) {
return typeof x == 'number' ? String(x) : x;
});

More From » lodash

 Answers
93

Lodash is much powerful, but for simplicity, check this demo





var data = [{
medicine: 1234,
info: blabla
}, {
medicine: 9585,
info: blabla
}];

dataMedicines = _.map(data, function(x) {
return _.assign(x, {
medicine: x.medicine.toString()
});
});

console.log(dataMedicines);

<script src=https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js></script>




[#61738] Wednesday, June 15, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mackenzihannal

Total Points: 548
Total Questions: 96
Total Answers: 96

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
;