【发布时间】:2020-09-27 16:10:43
【问题描述】:
我正在使用 this.state 中的建议变量显示建议
suggestions = this.state.products
.map((item) => item.name)
.sort()
.filter((v) => regex.test(v));
我的this.state.products 看起来像这样:
products = [
{
name: "Item1",
category: "stock",
id: 123
},
{
name: "Item1",
category: "product",
id: 456
},
{
name: "Item2",
category" "item"
}
]
但this.state.suggestions 只是看起来像这样(假设用户输入是 Item1 或接近那个):
suggestions = ["Item1", "Item1"]
我怎样才能让它看起来像:
suggestions = [
{
name: "Item1",
category: "stock"
},
{
name: "Item1",
category: "product"
}
]
【问题讨论】:
标签: javascript arrays reactjs sorting javascript-objects