将对象数组转换为TypeScript中的字符串数组

Abh*_*eet 1 javascript arrays lodash

使用lodash我需要将一个对象数组转换为字符串数组.

原始阵列,

const tags = [{
    "display": "tag1",
    "value": "tag1"
}, {
    "display": "tag2",
    "value": "tag2"
}]
Run Code Online (Sandbox Code Playgroud)

预期结果,

const tags = ["tag1", "tag2"]
Run Code Online (Sandbox Code Playgroud)

我这样试过,

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);
Run Code Online (Sandbox Code Playgroud)

Saj*_*ran 6

你不需要lodash,你可以使用普通JS使用map

DEMO

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

var result = tags.map(a => a.display);
console.log(result);
Run Code Online (Sandbox Code Playgroud)