【学习笔记-JS】处理阵列的方法

接下来介绍处理阵列的方法

这些方法对於资料处理很重要

那就一起来看下去吧

处理阵列的方法

.forEach()

阵列用来跑回圈,括号内放入function

.forEach() 内放入的function 可以回传三个值 item, index, array
item 为阵列内的值
index 为阵列内的索引
array 为阵列本身

    // 此程序码会跑阵列的长度的次数
    const array =  ['a', 'b', 'c', 'd', 'e']
    array.forEach(function (item, index, array){
    console.log(item, index, array)})
额外补充

名词解释匿名函式与箭头函式

    const array =  ['a', 'b', 'c', 'd', 'e']
    //没给function 名字称为匿名函式
    array.forEach(function (item, index, array){
    console.log(item, index, array)})
    // 把function 拿掉加入 => 称为箭头函式
    arr1.forEach((item, index, array) => {
      console.log(item, index, array)
    })

若使用.forEach() 想更改阵列内的值该如何修改
直接将item (阵列内的值)进行修改
console 出来後发现阵列内的值并未修改

    const array =  ['a', 'b', 'c', 'd', 'e']
    array.forEach((item, index, array) => {
      console.log(item, index, array)
      item = '1'
    })

需要指定阵列的索引值才能将阵列内的值逐一更改(写code 的时候需要注意一下)

    const array =  ['a', 'b', 'c', 'd', 'e']
    array.forEach((item, index, array) => {
      console.log(item, index, array)
      array[index] = '1'
    })

这个感觉有点像for of, for in 的感觉
补充:.forEach() 只能对一般阵列跑,有些资料回传资料型态看似阵列而非阵列则不可使用此方法(可能要踩几次雷才会知道其中的奥义)

.join()

把阵列使用指定的字串连接再一起

这个在资料处理上会搭配其他的方法一起使用

    const array = ['John', 'Cena', 'Good']
    console.log(array.join(' ')); // John Cena Good
    console.log(array.join('.')); // John.Cena.Good
    console.log(array.join('')); // JohnCenaGood

.push()

新增一个资料在阵列最後面,回传新增後的阵列长度(常使用到)

.push() 可以用来新增一笔资料,可以用到的地方太多(什麽都可以忘.push()不能忘!)

    const array = [1, 2, 3, 4, 5]
    array.push(9)
    console.log(array); // 回传新增的阵列 [1, 2, 3, 4, 5, 9]
    console.log(array.push(9)); // 回传长度 7

.unshift()

新增一个资料在阵列最前面,回传新增後的阵列长度

有新增到最後面的资料就有新增到最前面的资料,可以把他跟.push() 一起记起来

    const array = [1, 2, 3, 4, 5]
    array.unshift(6)
    console.log(array); // 回传新增的阵列 [6, 1, 2, 3, 4, 5]

.pop()

删除最後一笔资料,会回传删除的东西

有增加资料就有删除资料,这个跟 .push() 一样重要

    const array = [1, 2, 3, 4, 5]
    array.pop()
    console.log(array); // 回传删除的阵列 [ 1, 2, 3, 4]

.shift()

删除第一笔资料,会回传删除的东西

.shift() 跟 .unshift 可以一起记
顺便查一下单字的意思,应该是转移的意思,稍微想像一下把资料转移变成删除(?)
应该会好记一点

    const array = [1, 2, 3, 4, 5]
    array.shift()
    console.log(array); // 回传删除的阵列 [ 2, 3, 4, 5]

.splice()

从指定位置删除指定的资料值再新增资料

写了一堆看起来头更晕的注解,到底是什麽意思呢,直接看范例比较快

    // splice(起始位置,删除数量[新增值1,值2,值3])
    const array = [1, 2, 3, 4, 5]
    // 删除第 3 个位置 1 个数字再新增值 1, 1, 1
    // 删掉阵列的 4 再新增值
    array.splice(3,1,1,1,1)
    console.log(array); // 回传阵列 [1, 2, 3, 1, 1, 1, 5]

看范例好像有一点点看懂了
再多看几个范例吧

    const array = [1, 2, 3, 4, 5]
    // 在 0 的位置删除 0 个阵列在加入 0  
    array.splice(0, 0, 0)
    console.log(array); // 回传阵列 [0, 1, 2, 3, 4, 5]

.splice(0, 0, 0) 换言之就是在最前面新增一笔资料 0,跟刚刚提到的 .unshift()功能一样
所以 .splice() 也可以在最後加一笔资料跟.push()一样
当然也可以删除指定位置的资料也俱有.pop() .shift() 功能,只能说.splice() 使用弹性大


到这先歇一会儿
前面有提到字串的处理.split(), slice() 现在到了阵列处理多了.splice()
这三个东西真的长得很像加上英文普普,常常搞混到底要用哪一个
我们先来翻译一下这三个东西
.split() 分裂, .slice() 切片, .splice() 拼接
.split() 使用指定的字串切割文字
.slice(开始[,结束]) 切割文字,可以放入开始位置与结束位置(但不包含结束位置)

    const array = "1, 2, 3, 4, 5"
    const arrsplit = array.split(",")
    console.log(arrsplit); //  ['1', '2', '3', '4', '5']
    const arrslice = arrsplit.slice(2,4)
    console.log(arrslice); // ['3', '4']

好了感觉有清楚一点点了(吧?!)
继续介绍其他方法

.concat()

并两个阵列并产生新的阵列

放置array.concat(array1) 前後摆放会有差异,使用时要注意

    const array = [1, 2, 3]
    const array1 = [7, 8, 9]
    const array2 = array.concat(array1)
    console.log(array2); //  [1, 2, 3, 7, 8, 9]

.reverse()

将阵列反转

    const array = [1, 2, 3, 4, 5]
    array.reverse()
    console.log(array); //  [5, 4, 3, 2, 1]

.indexOf()

寻找阵列内是否有存在指定值,有则回传位置,无则回传 -1

字串处理也有.indexOf()
这个方法常用寻找是否有符合的资料

    const array = [1, 2, 3, 4, 5]
    const indexOf = array.indexOf(3)
    console.log(indexOf); //  2 找到数值 3 在位置 2
    const indexOf1 = array.indexOf(8)
    console.log(indexOf1); //  -1 找不到值

.includes()

确认阵列内是否有相符的值,并回传布林值

字串处理也有.includes()
可用来验证是否有相同的会员资料

    const array = [1, 2, 3, 4, 5]
    const includes = array.includes(3)
    console.log(includes); //  true
    const includes1 = array.includes(8)
    console.log(includes1); //  false

.some()

.some(item, index, array)

阵列用来跑回圈找出是否有判断的值

.some() 内放入的function 可以回传三个值 item, index, array
item 为阵列内的值
index 为阵列内的索引
array 为阵列本身
只要有一个值相符合就会回传true, 若都无相符则回传false

    array = [
      { name: 'class A', student: 22 },
      { name: 'class B', student: 25 },
      { name: 'class C', student: 30 },
      { name: 'class D', student: 35 },
    ]
    const has26student = array.some(item => {
        return item.student >= 26
    })
    console.log(has26student); // true

.findIndex()

.findIndex(item, index, array)

阵列用来跑回圈找出是否有判断值若有则回传第一个找到值的索引

    array = [
      { name: 'class A', student: 22 },
      { name: 'class B', student: 25 },
      { name: 'class C', student: 30 },
      { name: 'class D', student: 35 },
    ]
    const has30student = array.findIndex(item => {
      return item.student >= 30
    })
    console.log(has30student); // 2

.filter()

.filter(item, index, array)

阵列用来跑回圈找出是否有判断值并进行筛选并回传阵列内资料

    array = [
      { name: 'class A', student: 22 },
      { name: 'class B', student: 25 },
      { name: 'class C', student: 30 },
      { name: 'class D', student: 35 },
    ]
    const filtered = array.filter(item => {
      return item.student >= 30
    })
    console.log(filtered); // [{name:'class C',student:30},{name:'class D',student:35}]

.map()

.map(item, index, array)

重组阵列内容後产生新阵列,function 需return 新的值

    const array = [1, 2, 3, 4, 5]
    const array1 = array.map(item => {
        return item * 2
    })
    console.log(array1); // [2, 4, 6, 8, 10]

再看另外一个范例
将阵列的学生数目 * 2 并将所有阵列加入地址 New Taipei City

    array = [
      { name: 'class A', student: 22 },
      { name: 'class B', student: 25 },
      { name: 'class C', student: 30 },
      { name: 'class D', student: 35 },
    ]
    const array1 = array.map(item => {
        item.student *= 2
        item.address = 'New Taipei City'
        return item 
    })
    console.log(array1); 
    /* array = [
      { name: 'class A', student: 44, address: 'New Taipei City' },
      { name: 'class B', student: 50, address: 'New Taipei City' },
      { name: 'class C', student: 60, address: 'New Taipei City' },
      { name: 'class D', student: 70, address: 'New Taipei City' },
    ] */

.reduce()

.reduce(function, 初始值)

将阵列所有的值进行数学运算

做个加法的范例,function 内的运算式可依照自己需求运算

    const array = [1, 2, 3, 4, 5]
    const plus = (a, b) => {
      return a + b
    }
    const sum = array.reduce(plus)
    console.log(sum); // 15
    const sum1 = array.reduce(plus,5)
    console.log(sum1); // 20

.sort()

将阵列进行排序

function 内需传入两个值比大小并回转值
若 a - b < 0, a 会排在 b 前面
a - b > 0 则反之
详细验证可以youtube 搜寻相关影片

    const array = [1, 100, 20, 30, 15, 5]
    const array1 = array.sort((a,b) => {
        return a - b
    }
    console.log(array1); // [1, 5, 15, 20, 30, 100]
    const array2 = array.sort((a,b) => {
        return b - a
    }
    console.log(array2); // [100, 30, 20, 15, 5, 1]

分了好几次终於打完了,现在看到很多javascript 内建函式与方法,分开都听得懂(很模糊),但实际演练应用需要做相关程序逻辑的题目才会更了解使用方式,後续会再新增相关演练题目

拖了一阵子终於打完了,开心~


<<:  Re: 新手让网页 act 起来: Day30 - React hooks 之 useDebugValue

>>:  Day 30 完赛心得

Day 17 ( 中级 ) 立体空间 ( 三度空间 )

立体空间 ( 三度空间 ) 教学原文参考:立体空间 ( 三度空间 ) 这篇文章会介绍,如何在 Scr...

掌握SEO优化3大要点,让搜寻引擎知道「我就是你要的网站!」

网站上线後,开始接触网站经营相关的资讯时,才发现经营网站没那麽简单? 网站最重要的工作就是招揽潜在客...

【文字分析】3-4 TF-IDF文字概念

【文字分析】3-4 TF-IDF文字概念 说明 一种分析某单词在文章中重要程度公式 TF-IDF值与...

【Day25】[演算法]-合并排序法Merge Sort

合并排序法(Merge Sort)原理是会先将原始资料分割成两个资料列,接着再将两个资料继续分割成两...

[DAY6] 万事起头难

找救援 意识到有问题时,首先寻找有没有专案遇到同样的问题——有使用 Ruby on Rails 的大...