如果有未知密钥,如何在lodash中过滤

时间:2016-11-27 00:39:07

标签: reactjs lodash

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

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使用什么,或者我还有其他方法可以处理这种情况。

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

2 个答案:

答案 0 :(得分:1)

使用_.values

来避免未知密钥
_.filter(contacts, (user) => {
    return _.chain(user)
        .get('events')
        .values()
        .map('courseId')
        .includes(searchCourseID)
        .value();
});

答案 1 :(得分:1)

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

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()

,在没有lodash的情况下使用相同的逻辑
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);