1、数组的定义

//数组定义let names = ['book','pen','notes']//定义一个空数组let prices = []//push方法增加元素prices.push(300)prices

2、数组作为箭头函数参数 “...”称作为展开运算符

let names = ['book','pen','notes']let prices = []//使用push方法添加元素prices.push(300)prices.push('300')prices.push(20.5)console.log(`Fist product:${names[0]}:${prices[0]}`)//将数组prices的元素转为Number后进行求和运算let sumPrices = (...paras)=>paras.reduce((total,val)=>total + (Number.isNaN(Number(val)?0:Number(val))))let totalPrice = sumPrices(...prices)console.log(`Total price:${totalPrice} ${typeof totalPrice}`)//运行结果[Running] node "f:\jstest\test2.js"Fist product:book:300Total price:620.5 number[Done] exited with code=0 in 0.135 seconds

3、数组的合并和迭代

let combineArray = [...names,...prices] //数组合并//数组迭代combineArray.forEach(element=>    console.log(`Combined array element:${element}`)    )  //运行结果[Running] node "f:\jstest\test2.js"Fist product:book:300Total price:620.5 numberCombined array element:bookCombined array element:penCombined array element:notesCombined array element:300Combined array element:300Combined array element:20.5[Done] exited with code=0 in 0.134 seconds

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部