简体   繁体   中英

React JS render array items using lodash

I am trying to output only the first array using the _.find function of lodash. This works when I console.log the result. However I am struggling to input it into the render so that I can show the result in my JSX.

I have attempted numerous methods of extracting the array but haven't came to a successful conclusion. Any help fixing the issue and explain how to do so would be greatly appreciated.

The two const (events) do not run simultaneously, the top is the one I am attempting to run, the second is working but iterating through both arrays and showing the two sets of information on screen.

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

According to the lodash.find documentation , the correct way to use the shorthand to match a property value is:

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

or

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

so your code should be:

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM