js.数组

标准内置对象

常用函数 forEach 、filter、concat、find、some、every、map、includes、slice、reduce 、pop 、shift、unshift、push、 join、reverse、sort、splice、indexof

forEach遍历数组

  • forEach()最常见的用法是遍历数组,可以执行指定的函数来处理每个数组元素
    const arr = [1, 9, 8, 5, 4, 6]
    arr.forEach(it => console.log(it))

打印出1, 9, 8, 5, 4, 6

  • 获取数组索引
    在遍历数组时,可以使用forEach()方法的第二个参数来获取当前元素的索引,例如
    const arr = [1, 9, 8, 5, 4, 6]
    arr.forEach((item, index) => { console.log(item, index); })

1, 9, 8, 5, 4, 6
0, 1, 2, 3, 4, 5

push 末尾添加

接受任意类型的参数,将它们逐个添加到数组的末尾,并返回数组的长度

    const a = [1, 2, 3]
    a.push(6)
    console.log(a)

[1,2,3,6]

unshift 开头添加

在数组的前端添加任意个项,并返回数组的长度

   const arr = [1, 2, 3]
  arr.unshift(5)
    console.log(arr);

[5, 1, 2, 3]

pop删除末尾

在数组的前端添加任意个项,并返回数组的长度

    const arr = [1, 2, 3]
   arr.pop()
    console.log(arr);

[1, 2]

shift 删除开头

移除数组中的第一个项并且返回该项,同时将数组的长度减一

  const arr = [1, 2, 3]
    arr.shift()
    console.log(arr);

[2, 3]

splice 指定删除

可以实现删除、替换、修改
删除:指定两个参数(起始位置,要删除的项数)
插入:指定三个参数(起始位置,0,要插入的任意数量的项)
替换:指定三个参数(起始位置,要删除的项数,要插入的任意数量的项)
删除

   const arr = [1, 9, 8, 5, 4, 6]
  arr.splice(2, 1)
   console.log(arr);
   ```
   [1, 9, 5, 4, 6]
插入
```javascript
   const arr = [1, 9, 8, 5, 4, 6]
   arr.splice(5, 0, 6, 5, 4)
   console.log(arr);

[1, 9, 8, 5, 4, 6, 5, 4, 6]

替换

    const arr = [1, 9, 8, 5, 4, 6]
    arr.splice(0, 3, 1, 2, 3)
    console.log(arr);

[1, 2, 3, 5, 4, 6]

split 切割字符串返回数组

split() 方法接受一个模式,通过搜索模式将字符串分割成一个有序的子串列表,将这些子串放入一个数组,并返回该数组。

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split('');
console.log(words[3]);
// Expected output: "fox"

const chars = str.split('');
console.log(chars[8]);
// Expected output: "k"

const strCopy = str.split();
console.log(strCopy);
// Expected output: Array ["The quick brown fox jumps over the lazy dog."]

map 对数组每一项进行操作

对数组中的每一元素运行给定的函数,返回每次函数调用的结果组成的数组

  const arr = [1, 9, 8, 5, 4, 6]
    const arr2 = arr.map((item, index, arr) => item * 2)
    console.log(arr2);

[2, 18, 16, 10, 8, 12]

filter 过滤数组

对数组中的每一项运行给定的函数,会返回满足该函数的项组成的数组

    const arr = [1, 9, 8, 5, 4, 6]
    const arr2 = arr.filter(it => it > 5)
    console.log(arr2);

[9, 8, 6]

join 转换为字符串,并以括号内的字符串分割,返回字符串

   const arr = [1, 9, 8, 5, 4, 6]
    const arr2 = arr.join()
    console.log(arr2);

1,9,8,5,4,6

reverse 反转数组

反转数组项的顺序,改变原数组,返回新数组

    const arr = [1, 9, 8, 5, 4, 6]
    const arr2 = arr.reverse()
    console.log(arr2);

[6, 4, 5, 8, 9, 1]

replace 替换

replace() 方法返回一个新字符串,其中一个、多个或所有匹配的 pattern 被替换为 replacement。pattern 可以是字符串或 RegExp,replacement 可以是字符串或一个在每次匹配时调用的函数。如果 pattern 是字符串,则只会替换第一个匹配项。原始的字符串不会改变。

const paragraph = "I think Ruth's dog is cuter than your dog!";

console.log(paragraph.replace("Ruth's", 'my'));
// Expected output: "I think my dog is cuter than your dog!"

const regex = /Dog/i;
console.log(paragraph.replace(regex, 'ferret'));
// Expected output: "I think Ruth's ferret is cuter than your dog!"

sort 排序

默认排序:该方法会调用每个数组项的toString() 方法,然后按照字符序列排序
定义排序:该方法可以接受一个比较函数作为参数,比较函数有两个参数 .如果第一个参数位于第二个参数之前,返回负数 c.如果第一个参数位于第二个参数之后,返回正数

默认排序

    const arr = [1, 9, 8, 5, 4, 6, 11]
    const arr2 = arr.sort()
    console.log(arr2);

[1, 11, 4, 5, 6, 8, 9]

正序排序

    const arr = [1, 9, 8, 5, 4, 6, 11]
    const arr2 = arr.sort((a, b) => a - b)
    console.log(arr2);

[1, 4, 5, 6, 8, 9, 11]

倒序排序

    const arr = [1, 9, 8, 5, 4, 6, 11]
    const arr2 = arr.sort((a, b) => b - a)
    console.log(arr2);

[11, 9, 8, 6, 5, 4, 1]

concat 拼接数组

拼接数组,先创建当前数组的一个副本,然后将接收到的参数添加到这个副本的末尾,返回副本, 不改变原数组

    const arr = [1, 9, 8, 5, 4, 6, 11]
    const a = ['a', 'b', 'c']
    const arr2 = arr.concat(a)
    console.log(arr2);

[1, 9, 8, 5, 4, 6, 11, ‘a’, ‘b’, ‘c’]

slice数组切割

数组切割,可接受一个或者两个参数(返回项的起始位置,结束位置)
当接受一个参数,从该参数指定的位置开始,到当前数组末尾的所有项。
当接受两个参数,起始到结束之间的项,但是不包含结束位置的项。不改变原数组

一个参数

    const arr = [1, 9, 8, 5, 4, 6, 11]
    console.log(arr.slice(2))

[8, 5, 4, 6, 11]

两个参数

    const arr = [1, 9, 8, 5, 4, 6, 11]
    console.log(arr.slice(2, 5))

[8, 5, 4]

toString转换字符串

转换为字符串,在默认情况下都会以逗号分隔字符串的形式返回字符串

var arr = [1, 2, 3];
console.log(arr.toString());//返回'1,2,3'

every some 满足条件判断

every方法 测试所有 返回布尔值 数组.every((value, index) => {return 条件})
some 方法 测试至少一个 返回布尔值 数组.some((value, index) => { return 条件 })

    const arr = [1, 9, 8, 5, 4, 6, 11]
    const arr1 = arr.every((val, index) => (val > 6))
    console.log(arr1);  //false
    const arr2 = arr.some((val, index) => (val > 6))
    console.log(arr2) // true

find方法 查找元素和 findIndex方法 查找索引

find方法 查找元素 返回满足条件的项 语法 数组.find((value, index) =>return 条件))
findIndex方法 查找索引 返回满足条件的索引
findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。

let arr4 = [9, 4, 5, 23, 5]
        let a = arr4.find((val, index) => {
            return val === 9
        })
        console.log(a);
        // findIndex方法   查找索引   返回满足条件的索引
        let b = arr4.findIndex((val, index) => {
            return val === 5
        })
        console.log(b);

reduce 累计运算

reduce() 方法对数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。

  const arr = [1, 9, 8, 5, 4, 6, 11]
    let a1 = arr.reduce((sum, it) => (sum += it))
    console.log(a1);

44

includes查找数组是否包含元素

includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回 false。

   const arr = [1, 9, 8, 5, 4, 6, 11]
    console.log(arr.includes(9)); //ture
    console.log(arr.includes(2)); //false

indexOf查找值 和lastlndexOf 查找值(从后)

indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。

    const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];

    console.log(animals.lastIndexOf('Dodo'));  //3

    console.log(animals.lastIndexOf('Tiger')); //1

js中JSON.parse方法

JSON.parse一般用于将JSON字符串转化为JSON对象

在这里插入图片描述

在这里插入图片描述

JSON.stringify()对象转json字符串

JSON.stringify() 方法将一个 JavaScript 对象或值转换为 JSON 字符串,如果指定了一个 replacer 函数,则可以选择性地替换值,或者指定的 replacer 是数组,则可选择性地仅包含数组指定的属性。

console.log(JSON.stringify({ x: 5, y: 6 }));
// Expected output: '{"x":5,"y":6}'

console.log(
  JSON.stringify([new Number(3), new String('false'), new Boolean(false)]),
);
// Expected output: '[3,"false",false]'

console.log(JSON.stringify({ x: [10, undefined, function () {}, Symbol('')] }));
// Expected output: '{"x":[10,null,null,null]}'

console.log(JSON.stringify(new Date(2006, 0, 2, 15, 4, 5)));
// Expected output: '"2006-01-02T15:04:05.000Z"'


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值