最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

javascriptindexof方法实例讲解

来源:动视网 责编:小采 时间:2020-11-27 20:32:25
文档

javascriptindexof方法实例讲解

javascriptindexof方法实例讲解:indexOf()方法返回给定元素能找在数组中找到的第一个索引值,否则返回-1。 javascript indexof 语法 arr.indexOf(searchElement[, fromIndex = 0]) 参数 searchElement 要查找的元素 fromIndex 开始查找的位置。如果该索引值大于或等于数组
推荐度:
导读javascriptindexof方法实例讲解:indexOf()方法返回给定元素能找在数组中找到的第一个索引值,否则返回-1。 javascript indexof 语法 arr.indexOf(searchElement[, fromIndex = 0]) 参数 searchElement 要查找的元素 fromIndex 开始查找的位置。如果该索引值大于或等于数组


indexOf()方法返回给定元素能找在数组中找到的第一个索引值,否则返回-1。

javascript indexof 语法

arr.indexOf(searchElement[, fromIndex = 0])

参数

searchElement 要查找的元素

fromIndex 开始查找的位置。如果该索引值大于或等于数组长度,意味着不会在数组里查找,返回-1。如果参数中提供的索引值是一个负值,则将其作为数组末尾的一个抵消,即-1表示从最后一个元素开始查找,-2表示从倒数第二个元素开始查找 ,以此类推。 注意:如果参数中提供的索引值是一个负值,仍然从前向后查询数组。如果抵消后的索引值仍小于0,则整个数组都将会被查询。其默认值为0.

javascript indexof 描述

indexOf 使用strict equality (无论是 ===, 还是 triple-equals操作符都基于同样的方法)进行判断searchElement与数组中包含的元素之间的关系。

javascript indexof 实例

使用indexOf

以下例子使用indexOf方法确定多个值在数组中的位置。

var array = [2, 5, 9];
array.indexOf(2); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0

找出指定元素出现的所有位置

var array = [2, 5, 9];
array.indexOf(2); // 0
array.indexOf(7); // -1
array.indexOf(9, 2); // 2
array.indexOf(2, -1); // -1
array.indexOf(2, -3); // 0

判断一个元素是否在数组里,不在则更新数组

function updateVegetablesCollection (veggies, veggie) {
 if (veggies.indexOf(veggie) === -1) {
 veggies.push(veggie);
 console.log('New veggies collection is : ' + veggies);
 } else if (veggies.indexOf(veggie) > -1) {
 console.log(veggie + ' already exists in the veggies collection.');
 }
}

var veggies = ['potato', 'tomato', 'chillies', 'green-pepper'];

// New veggies collection is : potato,tomato,chillies,green-papper,spinach
updateVegetablesCollection(veggies, 'spinach'); 
// spinach already exists in the veggies collection.
updateVegetablesCollection(veggies, 'spinach');

indexof Polyfill

indexOf 在ECMA-262 标准 的第5版中被加入,但并非所有的浏览器都支持该方法。你可以在编写scripts时,在其开头使用以下代码,它能够允许你在没有本地支持的情况下使用indexOf方法。该算法符合ECMA-262第5版其中一项规定, 即假定 TypeErrorMath.abs 呈现它们原有的价值。

// Production steps of ECMA-262, Edition 5, 15.4.4.14
// Reference: http://es5.github.io/#x15.4.4.14
if (!Array.prototype.indexOf) {
 Array.prototype.indexOf = function(searchElement, fromIndex) {

 var k;

 // 1. Let O be the result of calling ToObject passing
 // the this value as the argument.
 if (this == null) {
 throw new TypeError('"this" is null or not defined');
 }

 var O = Object(this);

 // 2. Let lenValue be the result of calling the Get
 // internal method of O with the argument "length".
 // 3. Let len be ToUint32(lenValue).
 var len = O.length >>> 0;

 // 4. If len is 0, return -1.
 if (len === 0) {
 return -1;
 }

 // 5. If argument fromIndex was passed let n be
 // ToInteger(fromIndex); else let n be 0.
 var n = +fromIndex || 0;

 if (Math.abs(n) === Infinity) {
 n = 0;
 }

 // 6. If n >= len, return -1.
 if (n >= len) {
 return -1;
 }

 // 7. If n >= 0, then Let k be n.
 // 8. Else, n<0, Let k be len - abs(n).
 // If k is less than 0, then let k be 0.
 k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);

 // 9. Repeat, while k < len
 while (k < len) {
 // a. Let Pk be ToString(k).
 // This is implicit for LHS operands of the in operator
 // b. Let kPresent be the result of calling the
 // HasProperty internal method of O with argument Pk.
 // This step can be combined with c
 // c. If kPresent is true, then
 // i. Let elementK be the result of calling the Get
 // internal method of O with the argument ToString(k).
 // ii. Let same be the result of applying the
 // Strict Equality Comparison Algorithm to
 // searchElement and elementK.
 // iii. If same is true, return k.
 if (k in O && O[k] === searchElement) {
 return k;
 }
 k++;
 }
 return -1;
 };
}

文档

javascriptindexof方法实例讲解

javascriptindexof方法实例讲解:indexOf()方法返回给定元素能找在数组中找到的第一个索引值,否则返回-1。 javascript indexof 语法 arr.indexOf(searchElement[, fromIndex = 0]) 参数 searchElement 要查找的元素 fromIndex 开始查找的位置。如果该索引值大于或等于数组
推荐度:
标签: 方法 js 详解
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top