数组
- JavaScript数组元素索引未必连续,他们之间可能有空缺;
- a [-1.42] = true; //创建-1.42的属性
a ["1000"] = 0; //数组的第1001个元素
a [1.000]; //相当于 a [0]
3. 删除一个数组的元素可以这样组织代码:arr.length = 0;
4. 将数组的长度值设置为只读。var arr = [1,2,3]; Object.defineProperty(arr,"length",{writable : false});
5.
数组元素的添加和删除
1. var arr = []; arr.push("zero"); arr.push("one","two");
2. var arr = []; arr.push("zero"); arr.unshift("one","two"); //在数组头部插入元素 shift();第一个元素删除
3. var arr = [1,2,3]; delete arr[2]; //[1,2,undefined];
4. var arr = [1,2,3]; arr.pop(); //3
5. splice(i,n,elements); //i:从第几个开始;n:跨度几个元素;elements:if(n !==0) ?添加元素:删除;
6. Object.keys(obj); //获取索引(数组)
7. var arrObj = [1,2,"apple",4,5,"","banana"]; for(var i = 0,l =arrObj.length;i < l;i++){if(!arrObj[i]) continue;alert(i);}; //0,1,2,3,4,6 跳过5
8. 如果要跳过不存在元素而任然要处理undefined元素:for(var i = ...){if(!(i in arr)) continue;};
9. 跳过不是正整数的i:if(String(Math.floor(Math.abs(Number(i)))) !== i) {//todo};
10.code
var arrData = [1.44, 2.5, 3.07, 8.19, 5, 76, 33];
var sumOfSquares = 0;
for (var i = 0, len = arrData.length;i < len; i++) {
sumOfSquares += arrData[i] * arrData[i];
console.log("i值为: " + i + " " + sumOfSquares)
};