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

7
使用lodash,我需要将一个对象数组转换为字符串数组。
原始数组,
const tags = [{
    "display": "tag1",
    "value": "tag1"
}, {
    "display": "tag2",
    "value": "tag2"
}]

预期结果:

const tags = ["tag1", "tag2"]

我尝试了这种方法,
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);

1
你尝试了什么?而且,纯JS就可以做到,不需要使用lodash。 - Serge K.
1个回答

17

你不需要使用 lodash,你可以使用纯 JS 的 map 方法。

DEMO

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接