Array 工具函数只是想要使用起来方便
1.在数组中查找所有出现的元素x,并返回一个包含=>匹配索引的元素
// 查找数组a中的值1出现的位置,并返回数组
var a = [1, 2, 3, 4, 5, 6, 1, 1, 1, 4];
//result => [0,6,7,8]
function aIndex(a, v) {
var result = [],
len = a.length,
pos = 0;
while (pos < len) {
pos = a.indexOf(v, pos);
if (pos === -1) break;
result.push(pos);
pos = pos + 1;
}
return result;
}
var bb = aIndex(a, 1);
console.log(bb) // => [0,6,7,8]
2.类数组对象 =>真正的数组
var a = {
"0": 'a',
"1": 'b',
"2": 'c',
length: 3
}
var b = Array.prototype.slice.call(a, 0); // ["a", "b", "c"]
//把a转换成数组,并把字母改成大写字母
var B = Array.prototype.map.call(a,function(v){
return v.toUpperCase()
})
console.log(B) //=>["A", "B", "C"]