日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Array.prototype.myfindIndex

作者:web半晨 更新時間: 2022-06-06 編程語言


1、概念

findIndex()方法返回數組中滿足提供的測試函數的第一個元素的索引(下標)。若沒有找到對應元素則返回-1。


2、MDN鏈接地址

MDN - findIndex


3、示例代碼

let arrayData = [4, 6, 8, 12];

Array.prototype.myfindIndex = function(callback, context) {
	// 獲取第二個參數,
	// 即this指向。
	// 如果有直接使用,
	// 否則,指向window
	context = context || window;

	// 獲取this的長度。
	let len = this.length;

	// 初始化while的值。
	let i = 0;

	while (i < len) {
		// 調用函數
		if (callback.call(context, this[i], i, this)) return i;

		i++;
	};

	return -1;
};

let fun = function(item) { 
	return item + this.svalue > 10;
};

console.log(arrayData.myfindIndex(fun, { svalue: 5 }));
// 1

原文鏈接:https://blog.csdn.net/weixin_51157081/article/details/115794916

欄目分類
最近更新