使用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);
您不需要lodash,可以使用map使用纯JS
DEMO
const tags = [{
"display": "tag1",
"value": "tag1"
}, {
"display": "tag2",
"value": "tag2"
}]
var result = tags.map(a => a.display);
console.log(result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句