当前位置:网站首页>The method of array traversal in JS

The method of array traversal in JS

2022-06-09 11:26:00 Printf ('xiaobai ');

js Array traversal method in

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>
原网站

版权声明
本文为[Printf ('xiaobai ');]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091038289086.html