如何在javascript中实现lastIndexOf(array,value,[fromIndex])函数

时间:2015-08-04 17:26:47

标签: javascript underscore.js

lastIndexOf_.lastIndexOf(array,value,[fromIndex]) 返回数组中最后一次出现的值的索引,如果value不存在,则返回-1。传递fromIndex以在给定索引处开始搜索。

_.lastIndexOf([1,2,3,1,2,3],2); => 4

我的代码是:



function lastIndexOf(array,value){
	
	var temp=[];
	for(var i=0;i<array.length;i++){
		if(array[i]==value){
			return i;
		}
        return -1;
	}
	
}

console.log(lastIndexOf([1, 2, 3, 1, 2, 3],2));
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:0)

为什么不使用对数组工作正常的标准Javascript函数lastIndexOf

[1, 2, 3, 1, 2, 3].lastIndexOf(2)

结果

4

为什么要再次发明热水?

编辑:

为“发明者”做了这个编辑,不再发明递减循环

function lastIndexOf(array,value) {

    for(var i=array.length;i>0;i--){
      if(array[i]==value) return i;
    }
    return -1;
}

答案 1 :(得分:0)

取自ECMA-262 5.1 Edition/June 2011,第133-134页, 15.4.4.15 Array.prototype.lastIndexOf(searchElement [,fromIndex])。有自己的意见。

function lastIndexOf(O, searchElement, fromIndex) {
    var len, n, k, kPresent, elementK;
    // 1. Let O be the result of calling ToObject passing the this value as the argument.
    // ninascholz: given as parameter
    // 2. Let lenValue be the result of calling the [[Get]] internal method of O with the argument "length".
    // ninascholz: given as parameter
    // 3. Let len be ToUint32(lenValue).
    len = O.length;
    // 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 len-1.
    n = parseInt(fromIndex) || len - 1;
    // 6. If n = 0, then let k be min(n, len – 1).
    // ninascholz: should read n >= 0, otherwise the else condition would be wrong
    if (n >= 0) {
        k = Math.min(n, len - 1);
    } else {
        // 7. Else, n < 0
        // a. Let k be len - abs(n).
        k = len - Math.abs(n);
    }
    // 8. Repeat, while k= 0
    while (k >= 0) {
        // a. Let kPresent be the result of calling the [[HasProperty]] internal method of O with argument ToString(k).
        kPresent = k in O;
        // b. If kPresent is true, then
        if (kPresent) {
            // i. Let elementK be the result of calling the [[Get]] internal method of O with the argument ToString(k).
            elementK = O[k];
            // ii. Let same be the result of applying the Strict Equality Comparison Algorithm to searchElement and elementK.
            if (searchElement === elementK) {
                // iii. If same is true, return k.
                return k;
            }
        }
        // c. Decrease k by 1.
        k--;
    }
    // 9. Return -1.
    return -1;
}

function out(s) { var n = document.createElement('div'); n.innerHTML = s + '<br>'; document.getElementById('out').appendChild(n); }

// 4. If len is 0, return -1.
out(lastIndexOf([], 'e', -2)); // -1
out([].lastIndexOf('e', -2)); // -1

// 6. If n = 0, then let k be min(n, len – 1).
out(lastIndexOf(['a', 'b', 'c', 'd', 'e'], 'c', 10)); // 4
out(['a', 'b', 'c', 'd', 'e'].lastIndexOf('c', 10)); // 4

// 7. Else, n < 0
out(lastIndexOf(['a', 'b', 'c', 'd', 'e'], 'e', -2)); // -1
out(['a', 'b', 'c', 'd', 'e'].lastIndexOf('e', -2)); // -1

out(lastIndexOf(['a', 'b', 'c', 'd', 'e'], 'e', -1)); // 4
out(['a', 'b', 'c', 'd', 'e'].lastIndexOf('e', -1)); // 4

// ii. Let same be the result of applying the Strict Equality Comparison Algorithm to searchElement and elementK.
out(lastIndexOf([0, 1, 2, 3, 4, 5], '5')); // -1
out([0, 1, 2, 3, 4, 5].lastIndexOf('5')); // -1

out(lastIndexOf(['a', 'b', 'c', 'd', 'e'], 'e', 2)); // -1
out(['a', 'b', 'c', 'd', 'e'].lastIndexOf('e', 2)); // -1

// iii. If same is true, return k.
out(lastIndexOf(['a', 'b', 'c', 'd', 'e'], 'e')); // 4
out(['a', 'b', 'c', 'd', 'e'].lastIndexOf('e')); // 4
<pre id="out"></pre>

答案 2 :(得分:-1)

首先,关闭for循环。

其次,lastIndex没有意义,你是否从那个位置向后看?如果是的话,它将很容易实现。只是从那个地方看作是我的开始。

&#13;
&#13;
function lastIndexOf(array,value){
	
	var temp=[];
	for(var i=array.length-1;i>=0;i--){
		if(array[i]==value){
			return i;
		}
	}
    return -1;
}

console.log(lastIndexOf([1, 2, 3, 1, 2, 3],2));
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

function lastIndexOf(array,value){

  var temp;
  for(var i=0;i<array.length;i++){
      if(array[i]==value){
          temp = i;
      }
  }
  if (temp) return temp;
  return -1;
}