gpt4 book ai didi

javascript - React JS 使用 lodash 渲染数组项

转载 作者:行者123 更新时间:2023-12-03 03:21:10 24 4
gpt4 key购买 nike

我试图使用 lodash 的 _.find 函数仅输出第一个数组。当我 console.log 结果时,这有效。然而,我正在努力将其输入到渲染中,以便我可以在 JSX 中显示结果。

我尝试了多种提取数组的方法,但尚未得出成功的结论。任何帮助解决问题并解释如何做到这一点的帮助将不胜感激。

两个 const (事件)不会同时运行,顶部是我尝试运行的那个,第二个正在工作,但迭代两个数组并在屏幕上显示两组信息。

const cat = _.find(this.state.event, function(o) { return o.id == 1; });
console.log(cat);

//const events = _.find(this.state.event, ['id':1] ( => {
const events = this.state.event.map((item, i) => {
return <div>
<div className="center-cards margin-top margin-bottom test-cardSecond">
<div className="text-center margin-top">
<h1 className="card-heading">Scottish Conference 2017</h1>
<div className="card-sub">
<div>{item.description}</div><br />
{/* <div>Sale Date</div><div>{item.sales_end_time} - {item.sales_start_time}</div><br /> */}
</div>
</div>
</div>
<div className="belowBoxTestSecond">
<h1 className="bodyHeading">Example</h1>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. <br /><br />
Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. <br />
Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. <br />
Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? <br /><br />
Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
</div>
</div>
});

最佳答案

根据lodash.find documentation ,使用简写来匹配属性值的正确方法是:

_.find(myArray, ['property', value]);

_.find(myArray, {'property': value});

所以你的代码应该是:

const events = _.find(this.state.event, ['id', 1 ]).map(item => {

});

关于javascript - React JS 使用 lodash 渲染数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46569614/

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