TS实现原生数组查找方法:find()、findIndex()、includes()、indexOf()、lastIndexOf()


/**
 * @function Array.prototype.find()
 * @description 返回数组中满足提供的测试函数的第一个元素的值,否则返回undefined
 * @param predicate 接受三个参数:1.当前遍历的元素;2.当前遍历的索引值,可选;3.数组本身,可选
 * @param thisArg 可选 执行回调时用作 this 的对象
 * @returns any
 */
Array.prototype.find = function <T>(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any) {
  if (this == null) {
    throw new TypeError('"this" is null or not defined');
  }
  if (typeof predicate !== 'function') {
    throw new TypeError('predicate must be a function');
  }
  const arr = Object(this);
  const _len = arr.length;
  for (let i = 0; i < _len; i++) {
    if (predicate.call(thisArg || this, arr[i], i, arr)) {
      return arr[i];
    }
  }
  return undefined;
}

/**
 * @function Array.prototype.findIndex()
 * @description 返回数组中满足提供的测试函数的第一个元素的值的索引值,如果没有找到符合的元素则返回-1
 * @param predicate 接受三个参数:1.当前遍历的元素;2.当前遍历的索引值,可选;3.数组本身,可选
 * @param thisArg 可选 执行回调时用作 this 的对象
 * @returns number
 */
Array.prototype.findIndex = function <T>(predicate: (value: T, index: number, obj: T[]) => unknown, thisArg?: any) {
  if (this == null) {
    throw new TypeError('"this" is null or not defined');
  }
  if (typeof predicate !== 'function') {
    throw new TypeError('predicate must be a function');
  }
  const arr = Object(this);
  const _len = arr.length;
  for (let i = 0; i < _len; i++) {
    if (predicate.call(thisArg || this, arr[i], i, arr)) {
      return i;
    }
  }
  return -1;
}


/**
 * @function Array.prototype.includes()
 * @description 查找数组中是否包含某元素,如果包含返回true,不包含返回false
 * @returns boolean
 */
Array.prototype.includes = function (searchElement: any, position?: number) {
  if (this === null) {
    throw new TypeError('"this" is null or not defined');
  }
  const arr = Object(this);
  const _len = arr.length;
  for (let i = 0; i < _len; i++) {
    if (arr[i] === searchElement) {
      return true;
    }
  }
  return false;
}

/**
 * @function Array.prototype.indexOf()
 * @description 查找数组中是否包含某元素,如果包含返回第一个元素的索引值,不包含则返回-1
 * @returns number
 */
Array.prototype.indexOf = function (searchElement: any, position?: number) {
  if (this === null) {
    throw new TypeError('"this" is null or not defined');
  }
  const arr = Object(this);
  const _len = arr.length;
  for (let i = 0; i < _len; i++) {
    if (arr[i] === searchElement) {
      return i;
    }
  }
  return -1;
}

/**
 * @function Array.prototype.lastIndexOf()
 * @description 查找数组中是否包含某元素,如果包含返回最后一个元素的索引值,不包含则返回-1
 * @returns number
 */
Array.prototype.lastIndexOf = function (searchElement: any, position?: number) {
  if (this === null) {
    throw new TypeError('"this" is null or not defined');
  }
  const arr = Object(this);
  const _len = arr.length;
  for (let i = _len - 1; i >= 0; i--) {
    if (arr[i] === searchElement) {
      return i;
    }
  }
  return -1;
}

// test data
const testData = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1];
const findRes = testData.find(item => item > 4); // 5
const findIndexRes = testData.findIndex(item => item > 4); // 4
const includeRes = testData.includes(5); // true
const indexOfRes = testData.indexOf(5); // 4
const lastIndexOfRes = testData.lastIndexOf(5); // 6
console.log(findRes, findIndexRes, includeRes, indexOfRes, lastIndexOfRes)
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值