我想将此数据数组过滤为州和城市数组。我如何使用 lodash 或任何其他更好的方法而不是 for 循环和维护额外的数组来实现这一点。
data: [
{ id: 1, name: Mike, city: philps, state: New York},
{ id: 2, name: Steve, city: Square, state: Chicago},
{ id: 3, name: Jhon, city: market, state: New York},
{ id: 4, name: philps, city: booket, state: Texas},
{ id: 5, name: smith, city: brookfield, state: Florida},
{ id: 6, name: Broom, city: old street, state: Florida},
]
哪个用户点击 state
,出现状态列表。
{state: New York, count: 2},
{state: Texas, count: 1},
{state: Florida, count: 2},
{state: Chicago, count: 1},
当用户单击特定状态时,会出现该状态的 cities
列表。对于前。当用户点击纽约州时,
{id:1, name: Mike, city: philps}
{id:3, name: Jhon, city: market}
原文由 Balasubramanian 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用 lodash,您可以将
_.filter
与对象一起使用 --- [`.matches`](https://lodash.com/docs/#matches) iteratee shorthand_ for filtering the object with a given key/value pair and使用
_.countBy
和_.map
获取状态计数。