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"]