Я пытаюсь создать метод, который добавляется к объекту Array.prototype. Цель состоит в том, чтобы вернуть массив, который не включает значения индекса из массива, переданного моему методу.
Ниже приведены мои тестовые характеристики.
describe('doNotInclude', () => {
it('the doNotInclude method is added to the Array.prototype object', () => {
expect(typeof Array.prototype.doNotInclude).toBe('function');
});
it('returns an array', () => {
expect(Array.isArray([1, 2, 3, 4].doNotInclude(3))).toBe(true);
expect(Array.isArray([1, 2, 3, 4].doNotInclude([0, 2]))).toBe(true);
});
it('does not include the index values from the array passed to `doNotInclude`', () => {
expect([1, 2, 3, 4, 5].doNotInclude([3, 4])).toEqual([1, 2, 3]);
expect(
['zero', 'one', 'two', 'three', 'four', 'five', 'six'].doNotInclude([
0,
1,
])
).toEqual(['two', 'three', 'four', 'five', 'six']);
Мой код ниже:
Array.prototype.doNotInclude = function (arr){
return this.filter((elem, index) => {
if (!arr.includes(index)){
return elem;
}
})
}
Мой код не соответствует ни одной из спецификаций. Что я делаю неправильно?
Также, чтобы проверить мое концептуальное понимание, метод фильтра запускается на каком массиве? Это тот, который содержит индексы?