gpt4 book ai didi

javascript - 如何根据 react 中的类别过滤数组?

转载 作者:行者123 更新时间:2023-11-30 09:17:36 25 4
gpt4 key购买 nike

我正在学习 React 并试图弄清楚如何根据类别过滤数组。

我正在处理的数据是这样的: http://www.mocky.io/v2/5c2bd5683000006900abaff9

所以我可以使用这个过滤掉所有类别:

getTheCats() {
const cats = [...new Set(this.getVehicleData().map(v => v.category))];
const catsR = cats.filter(c => c).map((category, i) => {
// console.log(this.props.vehicle);

return (
<Col
xs="12"
key={i}
onClick={() => this.getCategories(category)}
className={
this.state.category === category
? 'border-bottom p-15 active'
: 'border-bottom p-15 not-active'
}
>
<FontAwesomeIcon
icon={this.state.active ? faMinusCircle : faPlusCircle}
/>
<h5 className=""> {category} </h5>
<SpecsDetails
key={this.props.vehicle.id}
vehicle={this.props.vehicle}
/>
</Col>
);
});
return (
<Row className="deal-details__specs accoridon-heading" id="specs">
{catsR}
</Row>
);
}

这给了我所有的个人类别。但我现在需要做的是将所有这些都放入 Accordion 中。所以类别实际上是 Accordion 的标题。我怎样才能得到符合每个类别的所有数据?

最佳答案

您可以使用 groupBy 获取类别对象,每个类别都有一个值数组。然后,您可以使用 toPairs 和带有 zipObjectmap 将其转换为类别对象数组:

const { pipe, groupBy, prop, toPairs, map, zipObj, dissoc } = R;

const groupByCategories = pipe(
groupBy(prop('category')),
map(map(dissoc('category'))), // optional - if you want to remove the category from the values
toPairs,
map(zipObj(['category', 'values']))
)

const data = [{"label":"Remote trunk/hatch release","category":"Comfort & Convenience","value":"Remote control trunk/hatch release"},{"label":"Cruise control","category":"Comfort & Convenience","value":"Included"},{"label":"Cargo area light","category":"Comfort & Convenience","value":"Included"},{"label":"Computer","category":"Comfort & Convenience","value":"Trip computer: includes average fuel economy and range for remaining fuel"},{"label":"Headlight control","category":"Comfort & Convenience","value":"Headlight control with dusk sensor"},{"label":"Power locks","category":"Comfort & Convenience","value":"Card key power locks ; automatic locking"},{"label":"Ventilation system","category":"Comfort & Convenience","value":"Ventilation system with micro filter"},{"label":"Secondary ventilation controls","category":"Comfort & Convenience","value":"Passenger ventilation controls"},{"label":"Air conditioning","category":"Comfort & Convenience","value":"Dual-zone climate control"},{"label":"Power windows","category":"Comfort & Convenience","value":"Front windows with one-touch on two windows, rear windows"},{"label":"Spare wheel","category":"Comfort & Convenience","value":"Included"},{"label":"Compass","category":"Comfort & Convenience","value":"Included"},{"label":"Smart card / smart key","category":"Comfort & Convenience","value":"Keyless Enter ‘n Go™ smart card/smart key with keyless entry"},{"label":"Vehicle start button","category":"Comfort & Convenience","value":"Included"},{"label":"External","category":"Dimensions","value":"L: 189.8, W: 76.5 - H: 69.3"},{"label":"Cargo area dimensions","category":"Dimensions","value":"Cargo area dimensions: loading floor height (inches): 32.4"},{"label":"Weight","category":"Dimensions","value":"6,500 (lbs)"},{"label":"Engine","category":"Engine","value":"3.6 v6 V"},{"label":"Fuel system","category":"Engine","value":"Multi-point fuel injection"},{"label":"Fuel Type","category":"Engine","value":"unleaded"}]

const result = groupByCategories(data)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>

关于javascript - 如何根据 react 中的类别过滤数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53998979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com