2017年4月3日
**计算给定数组 arr 中所有元素的总和 **
- arrayObject.slice(start,end)(递归使用)不修改原数组
- eval(string)
arrayObject.join(separator) - array.reduce(function(previousValue, currentValue, currentIndex, array), initialValue)
- forEach(function(value, index, array){})
- arrayObject.splice(index,howmany,item1,.....,itemX)
删除index后面的howmany个元素,并用itemx替换,修改原数组 - Math.pow(x,y)
返回x的y次幂 -
filter(value, idx, arr)
不用 filter() 时:
{"name":"apple", "count": 2},
{"name":"orange", "count": 5},
{"name":"pear", "count": 3},
{"name":"orange", "count": 16},
];
var newArr = [];
for(var i= 0, l = arr.length; i< l; i++){
if(arr[i].name === "orange" ){
newArr.push(arr[i]);
}
}
console.log("Filter results:",newArr);```
用了 filter():
```var arr = [
{"name":"apple", "count": 2},
{"name":"orange", "count": 5},
{"name":"pear", "count": 3},
{"name":"orange", "count": 16},
];
var newArr = arr.filter(function(item){
return item.name === "orange";
});
console.log("Filter results:",newArr);```
网友评论