根据另一个数组的属性过滤一个数组

时间:2019-01-27 21:12:16

标签: javascript arrays filter lodash

我确实有2个馆藏-事件预订enter image description here

事件包含事件的集合。 Bookings 集合仅包含我们需要预订的事件的eventId和当前登录的用户。

我使用lodash得到了尚未预订的活动

  const results = _.differenceWith(
          eventsArr,
          bookingArr,
          (event, booking) => event.id == booking.eventId
        );

我们如何选择预订的活动? 我想根据其他数组的eventID过滤事件数组,但是没有用!

任何想法都会有很大帮助!

编辑: 还要添加结构(感谢快速帮助,添加完整的结构可以帮助其他人,后端也是firebase)

事件数组

{
  "-LWSkZgZ-e84Aq7EnvOo" : {
    "date" : "January 17",
    "description" : "Lorem ipsum dolor amet fashion axe cray pour-over green juice. Salvia everyday carry viral, PBR&B pop-up polaroid direct trade gochujang hot chicken disrupt gentrify quinoa crucifix pabst cred. ",
    "imageUrl" : "https://images.pexels.com/photos/1047940/pexels-photo-1047940.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=500&w=500",
    "location" : {
      "lat" : 77.88,
      "lng" : 66.65,
      "name" : "Texas CA"
    },
    "name" : "MetalBone",
    "ticketsAvailable" : true
  },
  "-LWSkbMLqDlpTgcgFHy2" : {
    "date" : "January 18",
    "description" : "Mlkshk brooklyn gastropub paleo bicycle rights. Man bun brunch helvetica food truck whatever tousled vegan vinyl pug cred mumblecore. ",
    "imageUrl" : "https://images.pexels.com/photos/849/people-festival-party-dancing.jpg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=500&w=500",
    "location" : {
      "lat" : 32.77,
      "lng" : 96.7,
      "name" : "Dallas SF"
    },
    "name" : "Big Day Out",
    "ticketsAvailable" : true
  },

预订数组

{
  "-LWdae8S33xrHfLetvT7" : {
    "eventId" : "-LWSkZgZ-e84Aq7EnvOo",
    "userMail" : "test@test.com"
  },
  "-LWdj2UDTwVV6_71Bcyd" : {
    "eventId" : "-LWTraS93uC37S21syqP",
    "userMail" : "test@test.com"
  }
}

1 个答案:

答案 0 :(得分:2)

您可以使用_.intersectionWith()查找具有共同ID的事件/预订:

SELECT

此示例将获取您添加的数据,将其转换为数组格式,并使用const results = _.intersectionWith( eventsArr, bookingArr, (event, booking) => event.id === booking.eventId ); 查找预订的事件:

_intersectionWith()
const events = {"-LWSkZgZ-e84Aq7EnvOo":{"date":"January 17","description":"Lorem ipsum dolor amet fashion axe cray pour-over green juice. Salvia everyday carry viral, PBR&B pop-up polaroid direct trade gochujang hot chicken disrupt gentrify quinoa crucifix pabst cred. ","imageUrl":"https://images.pexels.com/photos/1047940/pexels-photo-1047940.jpeg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=500&w=500","location":{"lat":77.88,"lng":66.65,"name":"Texas CA"},"name":"MetalBone","ticketsAvailable":true},"-LWSkbMLqDlpTgcgFHy2":{"date":"January 18","description":"Mlkshk brooklyn gastropub paleo bicycle rights. Man bun brunch helvetica food truck whatever tousled vegan vinyl pug cred mumblecore. ","imageUrl":"https://images.pexels.com/photos/849/people-festival-party-dancing.jpg?auto=compress&cs=tinysrgb&dpr=1&fit=crop&h=500&w=500","location":{"lat":32.77,"lng":96.7,"name":"Dallas SF"},"name":"Big Day Out","ticketsAvailable":true}}

const bookings = {"-LWdae8S33xrHfLetvT7":{"eventId":"-LWSkZgZ-e84Aq7EnvOo","userMail":"test@test.com"},"-LWdj2UDTwVV6_71Bcyd":{"eventId":"-LWTraS93uC37S21syqP","userMail":"test@test.com"}}

const result = _.intersectionWith(
  _.map(events, (v, id) => ({ id, ...v })),
  _.values(bookings),
  (event, booking) => event.id == booking.eventId
)

console.log(result)