ES6——数组API

一、数组API

1.静态方法

  • Array.of(…args): 使用指定的数组项创建一个新数组
  • Array.from(arg): 通过给定的类数组 或 可迭代对象 创建一个新的数组。
const arr = Array.of(5);//值为5,length为1
const arr1 = new Array(10);//length为10的空数组
console.log(arr);
console.log(arr1);

在这里插入图片描述

const arr2 = {
    '0': 1,
    '1': 2,
    '2': 3,
    'length': 3,
    'push': Array.prototype.push,
    'splice': Array.prototype.splice
};
console.log(arr2);
const result = Array.from(arr2);
console.log(result);

在这里插入图片描述
在这里插入图片描述

2.实例方法

  • find(callback): 用于查找满足条件的第一个元素
  • findIndex(callback):用于查找满足条件的第一个元素的下标
  • fill(data):用指定的数据填充满数组所有的内容
  • copyWithin(target, start?, end?): 在数组内部完成复制
  • includes(data):判断数组中是否包含某个值,使用Object.is匹配
const arr = [{
        name: "d",
        id: 4
    },
    {
        name: "e",
        id: 5
    }]

//找到id为5的对象
const result = arr.find(item => item.id === 5);
const resultIndex = arr.findIndex(item => item.id === 5);
console.log(result, resultIndex);

在这里插入图片描述

// 创建了一个长度为10的数组,数组的每一项是"abc"
const arr = new Array(10);
arr.fill("abc");
console.log(arr); 

在这里插入图片描述

const arr = [1, 2, 3, 4, 5, 6];
//从下标2开始,改变数组的数据,数据来自于下标0位置开始
arr.copyWithin(2); // [1, 2, 1, 2, 3, 4]
// arr.copyWithin(2, 1); // [1, 2, 2, 3, 4, 5]
// arr.copyWithin(2, 1, 3); // [1, 2, 2, 3, 5, 6]
console.log(arr);
const arr = [45, 21, 356, 66 , 6, NaN, 723, 54,+0];
console.log(arr.indexOf(66) >= 0)
console.log(arr.includes(NaN));
console.log(arr.includes(-0));

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞羽逐星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值