Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  99] [ 7]  / answers: 1 / hits: 16789  / 7 Years ago, wed, january 3, 2018, 12:00:00

Using lodash I need to convert an array of object to the array of string.



Original array,



const tags = [{
display: tag1,
value: tag1
}, {
display: tag2,
value: tag2
}]


Expected result,



const tags = [tag1, tag2]


I tried this way,



const data = [{
display: tag1,
value: tag1
}, {
display: tag2,
value: tag2
}]

const result = _(data)
.flatMap(_.values)
.map((item) => { if (typeof item === 'string') { return item; } else { return; } })
.value()
console.log('result', result);

More From » arrays

 Answers
10

You dont need lodash, you can do with plain JS using map



DEMO



const tags = [{
display: tag1,
value: tag1
}, {
display: tag2,
value: tag2
}]

var result = tags.map(a => a.display);
console.log(result);




[#55557] Thursday, December 28, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
armandoh

Total Points: 208
Total Questions: 94
Total Answers: 112

Location: South Sudan
Member since Sun, Jul 11, 2021
3 Years ago
;