数组循环类及拼接类方法及去重

206 阅读4分钟

本文已参与[新人创作礼]活动,一起开启掘金创作之路

image.png

0.forEach(集合类)

a-返回值:没有

b:有不有return:没有

c:问题:如果想实现return

var arr = [1, 2, 3, 4, 5]
var newArr = arr.forEach((item) => {
  return item
})
console.log(newArr, 8)

输出结果

undefined 8

1. map(集合类)

a-返回值:有,返回一个新数组

b:有不有return:有,可以自定义返回值

var arr = [1, 2, 3, 4, 5]
var newArr = arr.map((item, index) => {
  return {
    id: item,
    index,
    gender: 1,
  }
})
console.log(newArr)

输出结果

[
  { id: 1, index: 0, gender: 1 },
  { id: 2, index: 1, gender: 1 },
  { id: 3, index: 2, gender: 1 },
  { id: 4, index: 3, gender: 1 },
  { id: 5, index: 4, gender: 1 }
]

2. filter(集合类)

a-返回值:有,返回一个新数组

b:有不有return:有,返回满足条件的那一项数据

var arr3 = [
  { id: 1, name: 'jack', age: 18 },
  { id: 2, name: 'rose', age: 28 },
  { id: 3, name: 'tom', age: 38 },
]
var newArr3 = arr3.filter((item) => {
  if (item.id == 1) {
    return item
  }
})
console.log(newArr3)

输出结果

[ { id: 1, name: 'jack', age: 18 } ]

3. every(集合类)

a-返回值:有,返回一个bool

b:有不有return:有,只要有一个不满足就是一个false

var arr5 = [
  { id: 1, name: 'jack', age: 18, flag: true },
  { id: 2, name: 'rose', age: 28, flag: false },
  { id: 3, name: 'tom', age: 38, flag: true },
]
var newArr5 = arr5.every((item) => {
  return item.flag
})
console.log(newArr5)

输出结果

false

4. some(集合类)

a-返回值:有,返回一个bool

b:有不有return:有,只要一项满足条件就立刻终止循环


 var arr4 = [
   { id: 1, name: 'jack', age: 18 },
   { id: 2, name: 'rose', age: 28 },
   { id: 3, name: 'tom', age: 38 },
 ] var newArr4 = arr4.some((item) => {
   return item.id == 4
 })
 console.log(newArr4)

输出结果

false

5. fill(拼接类)

1个参数:填充值。

const arr1 = [1, 2, 3, 4, 5]
arr1.fill(3) 
console.log(arr1) 

输出结果

3 3 3 3 3

2个参数:填充值+填充起始位置。

const arr2 = [1, 2, 3, 4, 5] 
arr4.fill(6, 3) 
console.log(arr2) 

输出结果

1,2,3,6,6

3个参数:填充值+填充起始位置+填充结束位置。

const arr3 = [1, 2, 3, 4, 5]
arr3.fill(6, 1, 3) 
console.log('%s', arr3) 

输出结果

1,6,6,6,5

6. findIndex(集合类)

有不有return:有,只要有条件满足就会返回索引


var arr7 = [
  { id: 1, name: 'lining', price: 100, num: 1 },
  { id: 2, name: 'anta', price: 200, num: 2 },
  { id: 3, name: 'nike', price: 300, num: 3 },
  { id: 4, name: 'tebu', price: 400, num: 1 },
]
var index = arr7.findIndex((item) => {
  return item.id === 1
})
console.log(index)

输出结果

0

7. find(集合类)

有不有return:有,只要有条件满足就会返回当前满足项

var arr8 = [
  { id: 1, name: 'n', price: 100, num: 1 },
  { id: 2, name: 'nn', price: 200, num: 2 },
  { id: 3, name: 'nnn', price: 300, num: 3 },
  { id: 4, name: 'nnnn', price: 400, num: 4 },
]
var obj = arr8.find((item) => {
  return item.id === 2
})
console.log(obj)

输出结果

{ id: 2, name: 'nn', price: 200, num: 2 }

8. reduce(集合类)

a-返回值:有,返回操作完毕后的最终值

c:参数1为表达式用于操作,常用求和;参数2为起始值

var arr6 = [
  { id: 1, name: 'n', price: 100, num: 1 },
  { id: 2, name: 'nn', price: 200, num: 2 },
  { id: 3, name: 'nnn', price: 300, num: 3 },
  { id: 4, name: 'nnnn', price: 400, num: 4 },
]
// var newArr6 = arr6.reduce((sum, item) => {
//   return sum += item.price * item.num
// }, 0)
var newArr6 = arr6.reduce((sum, item) => sum + item.price * item.num, 1)
console.log(newArr6)

输出结果

1801

9. slice(拼接类)

1个参数:截取数组中的数据返回一个新的数组,不会影响原数组

2个参数:从参数1开始,到参数2的前一位结束

var arr9 = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr9.slice(1, 3))

输出结果

[ 'b', 'c' ]

10. splice(拼接类)

1个参数:截取数组中的数据返回一个新的数组,会影响原数组

从当前参数的索引开始往后截取

打印原数组返回的是没有被截取的数组
var arr9 = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr9.splice(1))

输出结果

[ 'b', 'c', 'd', 'e', 'f' ]

2个参数:从参数1开始,到参数2的前一位结束 返回截取的数组,会影响原数组

var arr9 = ['a', 'b', 'c', 'd', 'e', 'f']
console.log(arr9.splice(1,3))

输出结果

[ 'b', 'c', 'd' ]

3个及3个以上参数,从第3个参数开始表示要替代到指定为的参数,会影响原数组,返回值为截取的数组

var arr9 = ['a', 'b', 'c', 'd', 'e', 'f']
arr9.splice(0, 3, 'd')
console.log(arr9)

输出结果

[ 'd', 'd', 'e', 'f' ]

10.split

用法

从特定字符或分隔符中分解或拆分字符串

    function fn(e){
        let arr = e.split(' ')
        console.log(arr);
        return arr
    }
    fn('HELLO IS ANOTHER WORLD')

输出结果

image.png

案例: 数组去重方法

1. Array.from

var newArr8 = [...new Set(arr)]
var newArr8 = Array.from(new Set(arr))
console.log(newArr8)

输出结果

[ 1, 2, 3, 4, 5 ]

2. reduce--includes

var arr = [1, 2, 3, 4, 5,1,2]
var newArr7 = arr.reduce((arr2, item) => {
  if (!arr2.includes(item)) {
    arr2.push(item)
  }
  return arr2
}, [])
console.log(newArr7)

输出结果

[ 1, 2, 3, 4, 5 ]

3. filter-indexOf

const arr=[ 1, 2, 3, 4, 5, 1, 2 ]
let newArr=arr.filter((item,index,arr)=>{ 
       return arr.indexOf(item,index+1)<0;
       }); 
    newArr.sort()
console.log(newArr)

输出结果

[ 1, 2, 3, 4, 5 ]

4.forEach--indexOf

   const arr=[ 1, 2, 3, 4, 5, 1, 2 ]
   let newArr=[]
   arr.forEach(e=>{
     if( newArr.indexOf(e)===-1)  
          newArr.push(e)
      })
   console.log(newArr)

5.双重for循环--splice(注:如果数组中是复杂数据类型,推荐使用这个)

var arr=[11,6,5,4,8,6,1,15,4,6];
  for(var i=0;i<arr.length;i++){
      for(var j=i+1;j<arr.length;){    // 将当前选项与之后选项作对比,删除重复的
          if(arr[i]==arr[j]){
              arr.splice(j,1);
              j--;
          }
          j++;
      }
        return arr;
}
console.log(arr1);