TypeScript 中的 findIndex 数组方法

新手上路,请多包涵

我是 Angular 4 开发的新手。我遇到的 someArray.findIndex() 不是函数。如何在 TypeScript 中添加这个 .findIndex() javascript 函数。

我正在构建一个日历组件,下面的函数给我错误

WEBPACK_IMPORTED_MODULE_2_lodash .findIndex 不是 CalendarComponent.isSelected (calendar.component.ts:4

 isSelected(date: moment.Moment): boolean {
    return _.findIndex(this.selectedDates, (selectedDate) => {
      return moment(date).isSame(selectedDate.mDate, 'day');
    }) > -1;
  }

太感谢了。

原文由 Vishwa 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 917
2 个回答

这与打字稿无关。 Typescript 中包含 Javascript 函数。您正在尝试使用 Lodash 功能。确保在文件顶部安装并导入它:

 import _ from 'lodash';

或者只使用 Javascript 函数:

 isSelected(date: moment.Moment): boolean {
    return this.selectedDates.findIndex((selectedDate) => {
      return moment(date).isSame(selectedDate.mDate, 'day');
    });
}

原文由 Patryk Gułaś 发布,翻译遵循 CC BY-SA 4.0 许可协议

selectedDate = arrayName.find(item => item.date === searchingdate);

原文由 Sarath Kumar 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题