3 回答
TA贡献1942条经验 获得超3个赞
您有一个对象,而不是数组。如果要获取值最低的属性名称,可以使用Object.entries(),Array.prototype.map()和Array.prototype.sort()这样:
const state = {
r1Price: '200',
r2Price: undefined,
r3Price: '1000',
r4Price: '100',
r5Price: '120'
};
const lowest = Object.entries(state)
.map(([k,v]) => [k, v ? +v : Infinity])
.sort((a, b) => a[1] - b[1])[0];
console.log(lowest);
然后,对于每个li,您都可以像这样绑定类:
render() {
const lowest = Object.entries(this.state)
.map(([k,v]) => [k, v ? +v : Infinity])
.sort((a, b) => a[1] - b[1])[0][0];
return (
<ul class="row">
<li className={'column r1 ' + lowest === 'r1Price'}>{...}</li>
<li className={'column r2 ' + lowest === 'r2Price'}>{...}</li>
<li className={'column r3 ' + lowest === 'r3Price'}>{...}</li>
</ul>
);
}
添加回答
举报