findIndex Array method in TypeScript

13,912

Solution 1

It's not about Typescript. Javascript functions are included in Typescript. You are trying to use Lodash function. Make sure that you installed and imported it on top of the file:

import _ from 'lodash';

or just use Javascript function:

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

Solution 2

You are calling findIndex method of _. You are either using UnderscoreJS or Lodash.

As the error says WEBPACK_IMPORTED_MODULE_2_lodash.findIndex is not a function It means you are using lodash.

Import lodash library and it will work.

import _ from 'lodash';

Solution 3

selectedDate = arrayName.find(item => item.date === searchingdate);
Share:
13,912

Related videos on Youtube

Vishwa
Author by

Vishwa

Updated on June 04, 2022

Comments

  • Vishwa
    Vishwa over 1 year

    I'm very new to Angular 4 development. I encounter someArray.findIndex() is not a function. How to add this .findIndex() javascript function in TypeScript.

    I'm building a calendar component, following function gives me error

    WEBPACK_IMPORTED_MODULE_2_lodash.findIndex is not a function at CalendarComponent.isSelected (calendar.component.ts:4

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

    Thank you so much.

  • Vishwa
    Vishwa over 5 years
    Thank you so much for your answer. This solved my problem. I've missed to install the lodash module.