JavaScript: Add Method to Array.prototype Object to Exclude Index Values from an Input Array
- 阅读更多 关于 JavaScript: Add Method to Array.prototype Object to Exclude Index Values from an Input Array
问题 I want to write code that adds the doNotInclude method to the Array.prototype object. The purpose of my code is: does not include the index values from the array passed to doNotInclude . Code below: Array.prototype.doNotInclude = function (arr) { //if (!Array.isArray(arr)) arr = [arr]; return this.filter((val, i) => { if (!arr.includes(i)) return val; }); }; ['zero', 'one', 'two', 'three', 'four', 'five', 'six'].doNotInclude([0, 1]) My code executes successfully and returns: [ 'two', 'three',