简体   繁体   English

ReactJS 映射+过滤或减少 [暂停]

[英]ReactJS map+filter or reduce [on hold]

I'm trying to make a simple booking app using react(fullcalendar.io) which is I'm kinda new我正在尝试使用 react(fullcalendar.io) 制作一个简单的预订应用程序,这有点新
What I want is if users book on the same date , users will be merged in 1 div我想要的是如果用户在同一日期预订,用户将被合并到 1 个 div 中

Basically on the image bellow user1 and user3 should be on the same div基本上在下面的图像上user1user3应该在同一个 div 上
Can you give me any tips on how to accomplish this?你能给我一些关于如何做到这一点的提示吗? 在此处输入图像描述

//App.js
state = {
  bookings: [
  ]
 }
 //Add Booking
 addBook = (name, ddate) => {
      let newBook ={
        id: uuid.v4(),
        name,
        ddate,
      }
      this.setState({bookings: [...this.state.bookings, newBook]})
 }
  render() {
    return (
      <div className="App">
      <div className="container">
        <AppCalendar addBook={this.addBook}/>
      </div>
      <div className="container-booking">
        <Bookings />
      <Booked  bookings={this.state.bookings} />
      </div>
    </div>
    )
  }
//AppCalendar.js
  state = {
        name: '',
    }

    //Prompt adding booking
    handleDateClick = (arg) => { // bind with an arrow function
        let name =  this.state.name;
        name = prompt("Name: ");
        this.props.addBook(name, arg.dateStr);
      }

    render() {
        return (
            <FullCalendar dateClick={this.handleDateClick} plugins={[ dayGridPlugin, interactionPlugin]} />
        )
    }
//Booked.js
render() {

       const bookingLists = this.props.bookings.map(booking => {
           return(
                <div key={booking.id}  style={bookingContainer}>
                   {booking.name } {booking.ddate} <br/><br/><br/>1/3
               </div>
           )
       })
        return (
            <div >
                {bookingLists}
            </div>
        )
    }
this.props.bookings.reduce((result, booking) => {
  const alreadyBookedDayIndex = result.findIndex((b) => moment(b.date).date() === moment(booking.date).date());
  if (alreadyBooekdDayIndex > -1) {
    result[alreadyBookedDayIndex].names.push(booking.name);
  } else {
    result.push({ names: [booking.name], date: booking.date });
  }
  return result;
}, []);

And then in view show然后在视图中显示

<div>{booking.names.join(' ')}&nbsp;{moment(booking.date).format('YYY-MM-DD')}</div>

MomentJS - check it out here . MomentJS -在这里查看

filter gives you a proper result in an array which can be rendered as it is using map : filter在数组中为您提供正确的结果,可以使用map进行渲染:

render() {
  return this.props.bookings
    .filter(booking => booking.date === this.state.selectedDate)
    .map(({ id, name, date }) => (
      <div key={id}>{name}, {date}</div>
    ))
  }

I think we need to first arrange the data from bookings array a bit.我认为我们需要先整理一下预订数组中的数据。 If you know about groupBy method from lodash, we can group data as per booking date and then map over that array and arrange jsx accordingly.如果您从 lodash 了解 groupBy 方法,我们可以根据预订日期对数据进行分组,然后在该数组上对 map 进行分组,并相应地安排 jsx。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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