ES6 数组的拓展(五)
一、扩展运算符(...)
将数组转化为以,分割的字符串
eg:
console.log(...[1,2,3,4]); //1 2 3 4
将字符串转化为数组
eg:
console.log([...'hello']); //['h','e','l','l','o']
求数组中最大值
eg:
//求参数中最大值
let result1 = Math.max(2,5,8,23,75);
console.log(result1);
//ES5中求数组中的最大值
let result2 = Math.max.apply(null,[2,5,8,23,75]);
console.log(result2); //
//ES6中求数组中的最大值
let result3 = Math.max(...[1,2,3,4,5,6,7]);
console.log(result3); //
二、数组新添方法
具备Iterator接口的数据结构:Array、String、类数组对象、Set和Map集合、
Array.from() 【将具备Iterator接口的数据结构转化为数组,并返回该数组】
Array.of() 【返回参数值组成的数组】
弥补构造函数创建数组的不足
eg:
let arr1 = new Array(10); //创建一个长度为10的空数组arr1
let arr2 = Array.of(10); //创建数组arr2 = [10];
find()和findIndex()
find() 【返回第一个符合条件的数组元素或undefined】
findIndex() 【返回第一个符合条件的数组元素的索引或-1】
eg:
let arr = [16,17,14,19,20,14,30]
//返回第一个符合条件的数组元素或undefined
let result = arr.find((element,index,arr)=>{
return element>18;
});
console.log(result); // //返回第一个符合条件的数组元素的索引或-1
let result2 = arr.findIndex((element,index,arr)=>{
return element>18;
});
console.log(result2); //
fill() 【使用给定参数值作为单个元素值替换数组中的所有元素】
eg:
[1,2,3].fill('hello'); //返回结果为:['hello','hello','hello']
arr.keys() 【返回包含所有元素索引的迭代器数组对象】
arr.values() 【返回包含所有元素值的迭代器数组对象】
arr.entries()【返回数组中元素索引、元素值以key-value形式的组成的迭代器数组对象】[[index1,element1],[index2,element2]]
注:
迭代器对象可以使用for-of来遍历获取里面的值
eg:
for(let [index,element] of arr.entries()){
console.log(index,element); //index为arr的元素索引,element为对应的索引的元素值
}
includes() 【判断数组中是否包含参数中的值,返回boolean类型】
eg:
[1, 2, 3].includes(2) // true
[1, 2, 3].includes(4) // false
[1, 2, NaN].includes(NaN) // true
ES6 数组的拓展(五)的更多相关文章
- ES6 数组方法拓展
ES6 数组方法拓展 1.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括E ...
- ES6数组的拓展
扩展运算符 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // 1 2 3 c ...
- ES6 随记(3.3)-- 数组的拓展
上一章请见: 1. ES6 随记(1)-- let 与 const 2. ES6 随记(2)-- 解构赋值 3. ES6 随记(3.1)-- 字符串的拓展 4. ES6 随记(3.2)-- 正则的拓展 ...
- 数组的复制及ES6数组的扩展
一.数组的复制 // alert([1,2,3]==[1,2,3]); let cc = [0,1,2]; let dd = cc; alert(dd==cc);//此时改变dd会影响cc ES5 只 ...
- es6数组的扩展
数组扩展运算符 ...(三个点) const demoArr=[0,1,2,3,4] console.log(...demoArr) // 0 1 2 3 4 // 他把一个数组用逗号分隔了出来 // ...
- ES6数组对象新增方法
1. Array.from() Array.from方法用于将两类对象转为真正的数组:类数组的对象( array-like object )和可遍历( iterable )的对象(包括 ES6 新增的 ...
- ES6数组扩展
前面的话 数组是一种基础的JS对象,随着时间推进,JS中的其他部分一直在演进,而直到ES5标准才为数组对象引入一些新方法来简化使用.ES6标准继续改进数组,添加了很多新功能.本文将详细介绍ES6数组扩 ...
- ES6数组及数组方法
ES6数组可以支持下面的几种写法: (1)var [a,b,c] = [1,2,3]; (2)var [a,[[b],c]] = [1,[[2],3]]; (3)let [x,,y] = [1,2,3 ...
- 数组复制的五种方式(遍历循环一一赋值、System.arraycopy、地址赋值、克隆clone()、Arrays.copyof())
package com.Summer_0424.cn; import java.util.Arrays; import java.util.concurrent.CopyOnWriteArrayLis ...
随机推荐
- DNS解惑之资源记录(2)
1.区域解析库 每个域都要维护一个区域解析库,而区域解析库都是由一条条的记录组成的,而每一条记录就被称为资源记录(resource record RR). 我们知道大多数域名下面都不仅仅有www服 ...
- 201871010107-公海瑜《面向对象程序设计(java)》第十四周学习总结
201871010107-公海瑜<面向对象程序设计(java)>第十四周学习总结 项目 内容 这个作业属于 ...
- 查看tensorflow是否为MKL版本命令
python -c "import tensorflow; print(tensorflow.pywrap_tensorflow.IsMklEnabled())" source a ...
- Emu8086三种格式的代码-(顺序,分支,循环)
这个学期准备考研,于是就没有怎么听别的课,现在临近期末,汇编成了个难题.下面是我学校的实验报告 做一个复习的记录吧,下面的代码都是在Emu8086上运行出来的代码 下面先介绍一下,汇编里面的格式问题 ...
- mysql使用记录
1. 报错 10061 将mysql启动即可
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- nginx ubantu 安装步骤
Ubuntu14.04默认安装的是Nginx 1.4.6 如果已经安装,请先卸载sudo apt-get remove nginx最新的稳定版Nginx 1.6.0在ubuntuupdates ppa ...
- cocos: RenderTexture 合并精灵图片
var render = new cc.RenderTexture(730, 450); //创建渲染纹理对象,并数字确定宽度 render.begin(); var sp1 = cc.Sprite. ...
- 【前端知识体系-CSS相关】CSS布局知识强化
1.实现两栏/三栏布局的方法? 表格布局 float + margin布局 inline-block布局 flexbox布局(兼容性的问题) 1.1 基础布局 <style> * { ma ...
- EasyNetQ笔记
Each call to Subscribe creates a new queue consumer. If you call Subscribe two times with the same m ...