Array.prototype.IndexOf = function (exp) { var isexp = exp.indexOf(‘=>‘) > -1, bef = isexp ? exp.split(‘=>‘)[0] : ‘‘, expstr = isexp ? exp.split(‘=>‘)[1].replace(new RegExp(bef, ‘g‘), ‘this[i].‘) : ‘‘; for (var i = 0; i < this.length; i++) { if (isexp && eval(expstr)) { return i; } else { if (this[i] == exp) { return i; } } } return -1; };
使用:
var
arr = [{ a: 1, b: 2 }, { a: 23, b: 987 }, { a: 23, b: 9837 }, { a: 2113, b: 987 }];
var
arr2 = [1,2,3,4,5];
console.log(arr2.indexOf(4)); //return 3
console.log(arr.indexOf(
"m=>m.a==23&&m.b==987"
)); //return 1
时间: 2024-11-01 23:53:37