React JS使用lodash渲染数组项

时间:2017-10-04 16:17:21

标签: javascript arrays reactjs lodash

我试图使用lodash的_.find函数仅输出第一个数组。这在我console.log结果时起作用。但是我很难将它输入到渲染中,以便我可以在JSX中显示结果。

我尝试了很多提取阵列的方法,但还没有成功结束。任何帮助解决问题和解释如何这样做将不胜感激。

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

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>
  });

1 个答案:

答案 0 :(得分:1)

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

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

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

所以你的代码应该是:

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

});