some(), every(), map(), find(), filter(), unshift() functions in JavaScript

1. some() and every() functions:

Some and every are iterative functions in JS. some returns true when it finds a condition that meets the conditions, and returns true only when every is satisfied. The content after return is the logic to be judged.

  • every() runs the given function on each item in the array, and returns true if the function returns true for each item.
  • some() runs the given function on each item in the array and returns true if the function returns true for any item.

parameter:

  1. item is the value obtained by each traversal
  2. index is the subscript of the current traversal
  3. array is the array itself
  4. return write judgment logic
var arr = [ 1, 2, 3, 4, 5, 6 ]; 

arr.some( function( item, index, array )=>{ 
    console.log( 'item=' + item + ',index='+index+',array='+array ); 
    return item > 3; 
}) 

arr.every(( item, index, array )=>{ 
    console.log( 'item=' + item + ',index='+index+',array='+array ); 
    return item > 3; 
})

Case: Implement a non-repeatable addition logic. In the search history, if a duplicate search record is entered, the search history should not add this field. So what is needed is to return true as long as there is one repeated field, not all of them, hence the use of some.


currentInfo //函数传递过来的当前搜索字段的id
//去重,some是有一个符合就立刻停止,返回true。every是全部的都满足下面的等式才会返回
const repeatFlag = historyArray.some(h => {
    return h.id === currentInfo.id
})

//返回true表示有该字段,直接return退出即可
if (repeatFlag)
    return

//将不重复的添加到cell中
historyArray.push(currentInfo)

 Second, the Map function:

Do the same for each element inside, and return a new array.

  • will not change the value of the original array
  • will return a new array

parameter:

  1. item is the value obtained by each traversal
  2. index is the subscript of the current traversal
  3. array is the array itself
  4. return write judgment logic
const arr = [1,2,3,4,5]

const arr_after_map = arr.map((currentVal,index,arrs)=>{
    return currentVal*currentVal
})

console.log("原数组的值为",arr)
console.log("修改之后的值为",arr_after_map)

Three, find (), filter () function

Both find and filter are search functions to find the target element.

The difference between filter and find: filter returns an array, and find returns an object .

Note: find () will not traverse the following elements after finding the first element, so if there are two identical elements in the array, he will only find the first one, and the second one will not be traversed again

const list = [{'name':'1',index:1},{'name':'2'},{'name':'1'}]

let list2 = list.find(i=>i.name==='1')

let list3 = list.filter(i=>i.name==='1')

console.log(list2); { name: '1', index: 1 }

console.log(list3);[ { name: '1', index: 1 }, { name: '1' } ]
  • find can not find return undefined
  • filter cannot find return []

Four, unshift () function:

Insert an element at the beginning of the array, push at the end, and unshift at the beginning.

let arr = [2,3]
arr.unshift(1)
arr.push(4)
console.log(arr) //[1,2,3,4]

Guess you like

Origin blog.csdn.net/weixin_60414376/article/details/127159978