当前位置:网站首页>The method of array traversal in JS
The method of array traversal in JS
2022-06-09 11:26:00 【Printf ('xiaobai ');】
foreach
- Traversal array ,
Once started, it cannot be stopped - Parameters can be two , A value representing each element , The other of the two represents index Value , You can also pass in other parameters to be placed in the following
<script> const arr = [' Xiaohong ',' bright red ',' Su Daqiang ',' Baby '] // foreach Once you start, you can't stop in the middle arr.forEach((item,index)=>{
console.log(item + '======' + index); if(item == ' Su Daqiang '){
console.log(' Su Daqiang's index number :'+ index); } }) </script>
some
- After the start
After the conditions are met, you can pass return true To end the traversal
<script> // some After finding the index return true The fixed writing method will not be executed later arr.some((item,index)=>{
console.log(item + '======' + index); if(item == ' Su Daqiang '){
console.log(' Su Daqiang's index number :'+ index); return true } }) </script>
every
There is a return value- Judge whether the conditions are met , If any item does not meet the conditions, it will return to false
<script> const arr1 = [ {
id:1 , name:' watermelon ', state:true}, {
id:2 , name:' durian ', state:false}, {
id:3 , name:' strawberry ', state:true} ] // Judge whether the conditions are met , If any item does not meet the conditions, it will return to false const result = arr1.every(item => item.state == true) console.log(result); </script>
filter
- Filter the elements in the array that meet the conditions
arr2.filter(item => item.state == true)
<script> const arr2 = [ {
id:1 , name:' watermelon ', state:true, price:10, count: 1}, {
id:2 , name:' durian ', state:false, price:80, count: 2}, {
id:3 , name:' strawberry ', state:true, price:20, count: 3} ] // Judge whether the conditions are met , If any item does not meet the conditions, it will return to false let amt = 0; arr2.filter(item => item.state == true).forEach(item => {
amt += item.price*item.count; }) console.log("amt: ",amt); </script>
reduce
- Accumulate each convenient value
- Format :
arr2.reduce(( Cumulative results , Current loop item )=>{return Result after accumulation }, Initial value )
<script> const arr2 = [ {
id:1 , name:' watermelon ', state:true, price:10, count: 1}, {
id:2 , name:' durian ', state:false, price:80, count: 2}, {
id:3 , name:' strawberry ', state:true, price:20, count: 3} ] // Add up the results of each cycle // arr2.filter(item => item.state == true).reduce(( Cumulative results , Current loop item )=>{return Result after accumulation }, Initial value ) const amt1 = arr2.filter(item => item.state == true).reduce((amt,item)=>{
return amt += item.price*item.count }, 0) // Another way of writing // const amt1 = arr2.filter(item => item.state == true).reduce((amt,item)=> amt += item.price*item.count, 0) console.log(amt1); </script>
边栏推荐
- 【基础知识】~ 硬核/软核/固核、PWM/SPWM、斐波那契数列、大端模式存储、傅里叶变换、奈奎斯特采样定律、芯片选型、基尔霍夫定律、FIR/IIR 滤波器
- This article takes you to understand gaussdb (DWS) [Gauss is not a mathematician this time]
- Object instantiation and access
- MKS H3615NS 直流电机驱动 使用说明书
- Enterprise distributed batch processing scheme based on task scheduling
- 最全知识总结,初学者必看
- Matlab related function knowledge points (III) -floor function + dot division operator + matrix index rules
- Easyrecovery15 free data recovery software
- Float float simulates double precision computation on CPU and GPU
- Introduction to automatic partition management of gaussdb (DWS)
猜你喜欢

Perfdog releases new indicators, tailored to the game

Harbor正确密码登录不上去

redis中的string类型是怎么组织的?

Camtasia 2022最新版新增功能

More than observation | Alibaba cloud observable Technology Summit officially launched

NFT market has entered the era of aggregation, and okaleido has become the first aggregation platform on BNB chain

对象的实例化和访问

TemplateDoesNotExist at /users/register/

MOS tube from entry to mastery

论文阅读 (54):DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks
随机推荐
Network planning | units of each layer in OSI model
[go] introduction to exp
一次内存泄漏的问题记录
float-float 模拟CPU和GPU上double双精度计算
How much do you know, deep analysis, worth collecting
建造者模式
Bidirectional BFS
[email protected] -808 loaded 5-fluorouracil| [email protected]
李宁抄底Clarks
Is it safe for the securities company with the lowest fees to open an account
MKS H3615NS 直流电机驱动 使用说明书
[email protected] -199 loaded drug ciprofloxacin
Openkruise v1.2: add persistentpodstate to realize stateful pod topology fixation and IP reuse
每日一题-1200. 最小绝对差
【基础知识】~ 稳压二极管、三极管、放大电路、逻辑门晶体管数量、FPGA 器件结温范围、FPGA 加载方式、施密特触发器、C 语言结构化编程、中断向量地址、寄生效应、上拉电阻的作用
Course design of network sniffer design based on C language
flutter 弹窗flutter_easyloading
[机缘参悟-20]:鬼谷子-反应篇-动静之术、说听结合、沉默是金
[go]实验包exp简介
使用五数概括法来确定数据集中的孤立点