6

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);
James Monger
  • 10,181
  • 7
  • 62
  • 98
abhijeetwebdev
  • 366
  • 1
  • 4
  • 14

1 Answers1

17

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);
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396