数组包含ES5和ES6

一、数组中的遍历

  • ES5中数组的遍历方法
1.for循环
var arr = [1,2,3,4]
for(let i =0;i<arr.length;i++){
  if(i == 0){
    continue; // 结束该次循环,打印结果为2,3,4
  }
  if(arr[i]>3){
    break; // 跳出循环,打印结果为2,3
  }
  console.log(arr[i])
}
2.forEach循环
var arr = [1,2,3]
arr.forEach(item=>{
  console.log(item)
})
// for和forEach的区别为:forEach写法更加简洁,但是forEach不支持continue与break,无法进行筛选
3.every
// 为了解决forEach的弊端,引入了every
var arr = [4,5,6]
arr.every(item=>{
  console.log(item)
  return true  // 通过return true/false可以中断遍历
})
arr.every(item=>{
  if(item>=6){
    return false;
  }
  console.log(item); // 打印结果为4
  return true; // 注意 需要return true进行放行
})
4.for in 
// 用于对象,不是所有的对象都是可以遍历的
var obj = {
  x: 1,
  y: 2,
  z: 3
}
for(let key in obj){
  console.log(key);
  console.log(obj[key])
}
  • ES6中数组的遍历方法
5.for of
用于非数组与对象,自定义的数据结构

二、数组的转换功能

数组的转换功能就是伪数组转换为数组。伪数组就是索引方式与数组一致,为自然数;有length属性。具有数组的特性,但是不能调用数组的方法

例如:{0: 'a',1: 'b',2: 'c',length: 3}
  • ES5的方法
let args = [].slice.call(arguments)
  • ES6的方法
let args = Array.from(arguments)
Array.from的用法
Array.from(arrayLike[, mapFn[, thisArg]]) // 参数为 伪数组对象,方法,this指向
eg: Array.from([1, 2, 3], x => x + x); // 结果为[2,4,6]

三、创建新数组

  • ES5
let arr = Array(5);
let arr = [1,2,3]
  • ES6
let arr = Array.of(1,2,3,4);
let arr = Array(5).fill(1)
let arr = Array.from({length: 5},function(){return 1}) // 与Array(5).fill(1)一致

四、数组中查找元素

filter // 筛选满足条件的元素,返回值为空或者非空

function isBigEnough(element) {
  return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44] 

find  // 不关注所有的值,不返回位置,只返回第一个满足条件的值,无则返回undefined

const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12

findIndex  // 返回位置,用法同find

some // 返回值为布尔值,至少有一个通过检测

const array = [1, 2, 3, 4, 5];
const even = (element) => element % 2 === 0;
console.log(array.some(even));
// expected output: true

五、判断是否为数组对象

Array.isArray(数组对象) // 返回值是布尔
Object.prototype.toString.call(arg)   // [object Array]

六、数组其他API

删除元素返回该元素的值,增加元素返回新数组的长度

  • 基本增加用法
var fruits = ['Apple', 'Banana'];

添加元素到数组的末尾

var newLength = fruits.push('Orange');
// newLength:3; fruits: ["Apple", "Banana", "Orange"]

添加元素到数组的头部

var newLength = fruits.unshift('Strawberry') // add to the front
// newLength: 1; fruits: ["Strawberry", "Banana"];

复制一个数组

var shallowCopy = fruits.slice(); // this is how to make a copy 
// ["Strawberry", "Mango"]

数组合并

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2); //['a', 'b', 'c','d', 'e', 'f']

使用splice添加元素

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

  • 基本删除用法
删除数组末尾的元素

var last = fruits.pop(); // remove Orange (from the end)
// last: "Orange"; fruits: ["Apple", "Banana"];

删除数组最前面(头部)的元素

var first = fruits.shift(); // remove Apple from the front
// first: "Apple"; fruits: ["Banana"];

找出某个元素在数组中的索引

fruits.push('Mango');
// ["Strawberry", "Banana", "Mango"]

var pos = fruits.indexOf('Banana');
// 1
通过索引删除某个元素

var removedItem = fruits.splice(pos, 1); // this is how to remove an item

// ["Strawberry", "Mango"]
从一个索引位置删除多个元素

var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'];
// ["Cabbage", "Turnip", "Radish", "Carrot"]

var pos = 1, n = 2;

var removedItems = vegetables.splice(pos, n);
// ["Cabbage", "Carrot"] (the original array is changed)

console.log(removedItems); 
// ["Turnip", "Radish"]
  • 修改数组
copyWithin() 方法浅复制数组的一部分到同一数组中的另一个位置,并返回它,不会改变原数组的长度。
arr.copyWithin(target[, start[, end]]) // 不包含end位置
const array1 = ['a', 'b', 'c', 'd', 'e'];
console.log(array1.copyWithin(0, 3, 4));
//  Array ["d", "b", "c", "d", "e"]

  • 数组排序
数组逆序

const array1 = ['one', 'two', 'three'];
const reversed = array1.reverse();
// expected output: "reversed:" Array ["three", "two", "one"]

sort排序

var numbers = [4, 2, 5, 1, 3];
numbers.sort(function(a, b) {
  return a - b;
});
// 结果为[1,2,3,4,5]
// sort 使用的是字母排序,return a-b即从小到大排序
  • 数组是否包含元素
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
console.log(beasts.indexOf('giraffe'));
// expected output: -1
  • 数组替换某值
const array1 = [1, 2, 3, 4];
// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]
  • 数组到字符串的转换
arr.join();
arr.toString();
Array.toLocaleString()
const array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')];
const localeString = array1.toLocaleString('en', {timeZone: "UTC"});
console.log(localeString);
// expected output: "1,a,12/21/1997, 2:12:00 PM",
  • 其他API
arr.entries(); // 遍历方法,返回值为键值对,涉及到iterator,先挖坑,后面埋
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转载:在开发中,数组的使用场景非常多,平日中也涉及到很多数组的api/相关操作,一直也没有对这块内容进行一块整理总...
    七色烟火阅读 3,239评论 0 3
  • ES5 数组方法 最近总结了一些ES5和ES6数组的一些方法,希望能给大家带来一些帮助吧 1.Array.isAr...
    前端的搬运工阅读 1,669评论 1 15
  • [TOC] 参考阮一峰的ECMAScript 6 入门参考深入浅出ES6 let和const let和const都...
    郭子web阅读 1,807评论 0 1
  • 什么是JavaScript JavaScript一种动态类型、弱类型、基于原型的客户端脚本语言,用来给HTML网页...
    zhaodadaya阅读 8,548评论 0 2
  • 1、新的声明方式 以前我们在声明时只有一种方法,就是使用var来进行声明,ES6对声明的进行了扩展,现在可以有三种...
    令武阅读 1,017评论 0 7