当存在未知密钥时,如何在lodash中进行过滤 [英] How to filter in lodash when there is an unknown key

查看:78
本文介绍了当存在未知密钥时,如何在lodash中进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按lodash中的子元素进行过滤.我使用firebase,它会为对象生成随机密钥.这里的数据结构是这样的:

I am trying to filter by child elements in lodash. I use firebase, which generates random keys for objects. The data structure here is like:

contact: {
  name: My Name,
  events: {
    -fdhu9euwf9hud: {
      eventName: day 1
      courseId: -3jfe2j09ej
    }
    -efdshkfhhiufd: {
      eventName: day 2
      courseId: -3jfe2j09ej
    }
    -fh9238fh9duf2: {
      eventName: day 1
      courseId: -dvh89wdfhoiw
    }
  }
}

我正在尝试查找属于某个课程的所有人员(和事件),并将其作为searchCourseID传递.我只是不知道下面的MYSTERYKEY用什么,或者是否有其他方法可以处理这种情况.

I am trying to find all people (and events) that belong to a certain course which will be passed in as searchCourseID. I just don't know what to use for MYSTERYKEY below or if there is another way I should be handling this situation.

contactsWithCourse = (
    _(this.props.contacts)
        .filter(({events}) => {
             if (events && events[MYSTERYKEY].courseId == searchCourseID) {
                return true
            } else {
                return false
            }
        }
)

推荐答案

您可以通过使用_.some()迭代事件来过滤结果,并检查其中是否至少有一个包含请求的searchCourseID.请注意,诸如_.some()_.filter()的收集方法可以在数组和对象(集合)上工作,并且可以忽略"对象中项目的(神秘)键:

You can filter the results using by iterating the events using _.some(), and check if at least one of them contains the requested searchCourseID. Note that collection methods, such as _.some() and _.filter() can work on arrays and objects (collections), and "ignore" the (mystery) keys of the items in the objects:

const result = _.filter(contacts, ({
  events
}) => _.some(events, ({
  courseId
}) => courseId === searchCourseID));

const searchCourseID = '-dvh89wdfhoiw';

const contacts = {
  '-uf39uhef2': {
    name: 'My Name',
    events: {
      '-fdhu9euwf9hud': {
        eventName: 'day 1',
        courseId: '-3jfe2j09ej'
      },
      '-efdshkfhhiufd': {
        eventName: 'day 2',
        courseId: '-3jfe2j09ej'
      },
      '-fh9238fh9duf2': {
        eventName: 'day 1',
        courseId: '-dvh89wdfhoiw'
      }
    }
  },
  '-willBeFiltered': {
    name: 'filtered out',
    events: {
      '-fdhu9euwf9hud': {
        eventName: 'day 1',
        courseId: '-3jfe123123'
      },
      '-efdshkfhhiufd': {
        eventName: 'day 2',
        courseId: '-3jfedf3433'
      },
      '-fh9238fh9duf2': {
        eventName: 'day 1',
        courseId: '-dvh8111111'
      }
    }
  }
};

const result = _.filter(contacts, ({
  events
}) => _.some(events, ({
  courseId
}) => courseId === searchCourseID));

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>

使用ES6 Object.values(),同样的逻辑也可以不使用破折号:

The same logic works without lodash, using ES6 Object.values():

const result = Object.values(contacts).filter(({
  events
}) => Object.values(events).some(({
  courseId
}) => courseId === searchCourseID));

const searchCourseID = '-dvh89wdfhoiw';

const contacts = {
  '-uf39uhef2': {
    name: 'My Name',
    events: {
      '-fdhu9euwf9hud': {
        eventName: 'day 1',
        courseId: '-3jfe2j09ej'
      },
      '-efdshkfhhiufd': {
        eventName: 'day 2',
        courseId: '-3jfe2j09ej'
      },
      '-fh9238fh9duf2': {
        eventName: 'day 1',
        courseId: '-dvh89wdfhoiw'
      }
    }
  },
  '-willBeFiltered': {
    name: 'filtered out',
    events: {
      '-fdhu9euwf9hud': {
        eventName: 'day 1',
        courseId: '-3jfe123123'
      },
      '-efdshkfhhiufd': {
        eventName: 'day 2',
        courseId: '-3jfedf3433'
      },
      '-fh9238fh9duf2': {
        eventName: 'day 1',
        courseId: '-dvh8111111'
      }
    }
  }
};

const result = Object.values(contacts).filter(({
  events
}) => Object.values(events).some(({
  courseId
}) => courseId === searchCourseID));

console.log(result);

这篇关于当存在未知密钥时,如何在lodash中进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆