forEach(function(value,index){
})
forEach()返回的值首先是数组元素的值,其次是元素的索引
each(function(index,value){
})
each()返回的值首先是数组元素的索引,其次是元素的值
each可以遍历伪数组
数组的某个位置是空位,与某个位置是undefined
,是不一样的。
undefined
,遍历的时候就不会被跳过。
push
和pop
结合使用,就构成了“后进先出”的栈结构(stack)。
function findLongest(arr){
return arr.reduce((longest, next) => {
return longest.length > next.length? longest: next
})
}
const arr = [‘2222‘,‘222222‘,‘11111111111111‘]
console.log(findLongest(arr)); // 11111111111111
原文:https://www.cnblogs.com/yxp2918/p/14191099.html