重学ES6(一):数组及其新方法

数组的遍历

1.传统for循环

2.forEach方法,不支持break和continue

const arr = [1, 2, 3, 4, 5]

arr.forEach(function (item) {
  console.log(item)
})

3.every方法,可以做到 break 那样的效果,return false 等同于 break,return true 等同于 continue

const arr = [1, 2, 3, 4, 5]
// every 默认return false(即遍历一项后跳出),要一直遍历需要加 return true
// return false 等同于 break,return true 等同于 continue
arr.every(function (item) {
  if (item === 2) {
    return false
  }
  console.log(item)
  return true
}) // 1 3 4 5

4.for…in可以遍历数组,而且还支持 continue、break等功能,但是会遍历出自定义属性,更适合用于对象的遍历

 const arr = [1, 2, 3, 4, 5]

// for in 主要为object进行遍历
// for in 为数组遍历时,如果有自定义属性,会遍历出自定义属性
// for in 支持break和continue
// arr.a = 100 遍历后会有 a 100
for (let index in arr) {
  console.log(index, arr[index])
} 

5. for…of可遍历一切可遍历的数据结构

 const arr = [1, 2, 3, 4, 5]

// for of (ES6新增)
// for…of 遍历的是一切可遍历的元素(数组、对象、集合)等
for (let item of arr) {
  console.log(item)
}

伪数组(Array-Like)

函数中的 arguments、DOM中的 NodeList等,当然,还有一些可遍历的对象,看上去都像数组却不能直接使用数组的 API,它们是伪数组(Array-Like)。要想对这些对象使用数组的 API 就要想办法把它们转化为数组。

1.伪数组具备两个特征:

(1)按索引方式储存数据

(2)具有length属性

let arrLike = {
0: ‘a‘,
1: ‘b‘,
2: ‘c‘
}

2.ES5    arrayLike ---> array  通常使用[].slice.call

// ES5
let args = [].slice.call(arguments) // collection
let imgs = [].slice.call(document.querySelectorAll(‘img‘)) // NodeList

3.ES6    arrayLike ---> array  通常使用Array.from

// ES6
let args = Array.from(arguments)
let imgs = Array.from(document.querySelectorAll(‘img‘))

创建数组

1.Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。

//只填写一个参数时Array.of()创建一个具有单个元素的包含该元素的数组
Array.of(7);       // [7]
Array.of(1, 2, 3); // [1, 2, 3]

//只填写一个参数时Array()创建长度为参数的空数组
Array(7);          // [ , , , , , , ]
Array(1, 2, 3);    // [1, 2, 3]

2.Array.fill() 填充数组,用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。

// Array.fill() 填充数组
let array = Array(5).fill(9)
console.log(array) // [9, 9, 9, 9, 9]

// Array.fill(value,start,end)
// value用来填充数组元素的值,start起始索引,end终止索引
// fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。
let array = [0, 11, 22, 33, 44, 55, 66, 77]
console.log(array.fill(‘replace‘, 1, 4)) // [0, "replace", "replace", "replace", 44, 55, 66, 77]

数组元素查找

1.ES5   filter()方法,返回符合测试函数的数据组成的数组,否则返回空数组

// ES5
let array = [0, 11, 22, 33, 44, 55, 66, 77]
// filter()方法,返回符合测试函数的数据组成的数组,否则返回空数组
// filter()方法会找出所有符合测试函数的数据组成数组返回,用于验证数据是否存在时效率低
let find = array.filter(function (item) {
    return item === 44
})
console.log(find) // [44]

2.ES6 find() 方法返回数组中满足提供的测试函数的第一个元素的值,否则返回 undefined。

// ES6
let array = [0, 11, 22, 33, 44, 55, 66, 77]
// find() 方法返回数组中满足提供的测试函数的第一个元素的值,否则返回 undefined
// find() 用于验证数据是否存在时效率高
let find = array.find(function (item) {
  return item === 22
})
console.log(find) // 22

3. ES6 findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。

// findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。其实这个和 find() 是成对的,不同的是它返回的是索引而不是值。
let array = [0, 11, 22, 33, 44, 55, 66, 77]
let findex = array.findIndex(function (item) {
  return item === 22
})
console.log(findex) // 2

4. filter(),find(),findIndex()比较

filter()用于找出所有匹配的元素的数据

find()主要用于验证元素是否存在

findIndex()找出数组中满足提供的测试函数的第一个元素的索引

原文地址:https://www.cnblogs.com/xzweb/p/12259112.html

时间: 2024-08-13 01:53:03

重学ES6(一):数组及其新方法的相关文章

ES6中数组的新方法

数组的扩展 1.1扩展运算符 1.1.1:... 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. <body> <div></div> <div></div> <div></div> <div></div> </body> <script type="text/javascript"> c

vue学习(十四) 条件搜索框动态查询表中数据 数组的新方法

//html <div id="app"> <label> 名称搜索关键字: <input type="text" clasa="form-control" v-model="keywords"> </label> <table class="table table-bordeered table-hover table-striped"> <

数组一些新方法

1.Array.prototype.forEach() forEach() 方法让数组的每一项都执行一次给定的函数. 语法: array.forEach(callback[, thisArg]) 参数 callback 在数组每一项上执行的函数,接收三个参数:  currentValue 当前项(指遍历时正在被处理那个数组项)的值. index 当前项的索引(或下标). array 数组本身. thisArg 可选参数.用来当作callback 函数内this的值的对象. 示例 var arr

转 JavaScript里的数组转化新方法Array.From

过去,我们使用各种各样的方法来将类似Array对象的东西(比如 arguments 和 NodeList) 转换成真的数值.比如下面这个: 将NodeList转换成数组 基本上,这些东西我们都可以使用Array.prototype.slice.call()这样的方法将arguments和NodeList等转行成想要的形式. 如今,我们有了更直接的方法,直接将这些类数组的对象转化成真正的对象. 将NodeList 转化成 Array var divs = Array.from(document.q

javaScript中 数组的新方法(reduce)

定义和用法 reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值. reduce() 可以作为一个高阶函数,用于函数的 compose. 注意: reduce() 对于空数组是不会执行回调函数的. 浏览器支持 表格中的数字表示支持该方法的第一个浏览器版本号. 语法 :array.reduce(function(total, currentValue, currentIndex, arr), initialValue) 参数 栗子1: var arr

重学ES6(二):ES5和ES6中Class类的相同与不同

ES5和ES6中Class类的相同与不同 先说结论,简而言之ES5用function定义类,ES6用class定义类,class的本质是function,ES6中的类只是语法糖,它并没有改变ES5下类实现的本质. 类的定义 ES5 // ES5函数来描写类 // 声明类 let Animal = function (type) { this.type = type // 定义实例方法 this.drink = function () { console.log('drink') } } // 定

ECMA5数组的新方法forEach()模仿实现

1 var o = { 2 forEach: function (callback) { 3 // alert(this.length); 4 for (var i = 0, len = this.length; i < len; i++) { 5 callback && callback(this[i], i, this); 6 } 7 }, 8 get length(){ 9 var sum=0; 10 for(var n in this) { 11 sum+=1; 12 } 1

重学ES6(三):函数和箭头函数

函数的参数默认值 ES5---参数的默认值通常都是写在函数体中,不传参即用默认参数 // ES5 // 设置参数默认值,未传入参数时按照默认值 function f(x, y, z) { if (y === undefined) { y = 7 } if (z === undefined) { z = 42 } return x + y + z } console.log(f(1)) // 50 console.log(f(1, 8, 43)) // 52 原文地址:https://www.cn

JavaScript笔记6-数组新方法

七.ECMAScript5关于数组的新方法 1.forEach():遍历数组,并为每个元素调用传入的函数;     举例: 1 var a = [1,2,3]; 2 var sum = 0; 3 //传一个参数 4 a.forEach(function(v){ 5 sum += v; 6 }); 7 console.log(sum);//6 8 //传三个参数(元素值,索引,数组本身) 9 a.forEach(function(v,i,a){ 10 a[i]=v+1;//为数组的各元素自加1 1