es6数组方法有什么
时间:2022-02-11 16:08
es6数组方法有:“from()”、“of()”、“copyWithin()”、“fill()”、“find()”、“findIndex()”、“includes()”、“entries()”、“keys()”、“values()”等。 本教程操作环境:windows7系统、ECMAScript 6版、Dell G3电脑。 splice 语法 slice 语法 索引方法 indexOf lastIndexOf 迭代方法 每个方法都接受两个参数,第一个参数callback(回调函数,必选),第二个参数是一个可选的上下文参数。 需要注意的是除了forEach()方法,其余的迭代方法中callback需要有return值否则会返回undefined。 forEach forEach()对数组进行遍历循环,对数组中的每一项运行给定的函数,这个方法没有返回值。 map map()指“映射”,对数组中的每一项运行给定的函数,返回每次函数调用的结果组成的数组。 filter filter(),“过滤”,对数组中的每一项运行给定函数,返回满足过滤条件组成的数组。 every every(),判断数组中每一项都是否满足所给条件,当所有项都满足条件,才会返回true。 some some(),判断数组中是否存在满足条件的项,只要有一项满足条件,就会返回true。 归并方法 这两个方法相比前面可能稍微复杂一些,它们都会迭代数组中的所有项,然后生成一个最终返回值。这两个方法接收两个参数。 reduce reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。 reduce() 对于空数组是不会执行回调函数的。 reduceRight reduceRight()和reduce()相比,用法类似,差异在于reduceRight是从数组的末尾开始实现的。 Array方法 isArray 判断参数是否是”Array”返回true或false。 Array方法 Array.from() 用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。 Array.from还可以接受第二个参数,作用类似于数组的map方法,用来对每个元素进行处理,将处理后的值放入返回的数组。 Array.of() 用于将一组值,转换为数组。 实例方法 会改变原数组 copyWithin() 在当前数组内部,将指定位置的成员复制到其他位置(会覆盖原有成员),然后返回当前数组。 fill() 使用给定值,填充一个数组。 不会改变原数组 find() 用于找出第一个符合条件的数组成员。它的参数是一个回调函数,所有数组成员依次执行该回调函数,直到找出第一个返回值为true的成员,然后返回该成员。如果没有符合条件的成员,则返回undefined。 findIndex() findIndex方法的用法与find方法非常类似,返回第一个符合条件的数组成员的位置,如果所有成员都不符合条件,则返回-1。 includes() 返回一个布尔值,表示某个数组是否包含给定的值。 entries(),keys() 和 values() ES6提供了三个新方法:entries()、keys()和values(),用来遍历数组。它们都返回一个遍历器对象,可以用for...of循环进行遍历,唯一的区别是keys()是对数组的键名的遍历、values()是对数组键值的遍历,entries()方法是对数值的键值对的遍历。 如果不使用for...of循环,可以手动调用遍历器对象的next方法,进行遍历。 【推荐学习:javascript高级教程】 以上就是es6数组方法有什么的详细内容,更多请关注gxlsystem.com其它相关文章!传统Array 对象方法
修改原数组 不修改原数组 push, pop concat unshift,shift join sort slice reverse indexOf(),lastIndexOf() splice forEach copyWithin map fill filter some every reduce,reduceRight includes finde,findIndex entries(),keys(),values() 修改原数组
arrayObject.splice(index,howmany,item1,.....,itemX)
var arr = new Array();
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr.splice(2,1); //"George", "John"
arr.splice(1,0,"William"); //"George", "William", "John"
arr.splice(2,1,"haha"); //"George", "William", "haha"
不修改原数组
arrayObject.slice(start,end);
var arr = new Array();
arr[0] = "George";
arr[1] = "John";
arr[2] = "Thomas";
arr.slice(2,1); //[]
arr.slice(1,2); //"William"
arr.slice(-2,-1); //"William"
将类似数组的对象(比如arguments)转换为真实的数组
Array.prototype.slice.call(arguments);ES5新增数组
方法并不会修改原数组
array.indexOf(searchElement[, fromIndex])
var data = [2, 5, 7, 3, 5];
console.log(data.indexOf(5, "x")); // 1 ("x"被忽略)
console.log(data.indexOf(5, "3")); // 4 (从3号位开始搜索)
array.lastIndexOf(searchElement[, fromIndex])
var data = [2, 5, 7, 3, 5];
console.log(data.lastIndexOf(5)); // 4
console.log(data.lastIndexOf(5, 3)); // 1 (从后往前,索引值小于3的开始搜索)
console.log(data.lastIndexOf(4)); // -1 (未找到)
两个方法在比较第一个参数与数组中的每一项时,会使用全等操作符, 要求必须完全相等,否则返回-1。
[].forEach(function(value, index, array) {
// ...
}, [ thisObject]);
[1, 2, 3, 4].forEach(function (item, index, array) {
console.log(array[index] == item); // true
sum += item;
});
alert(sum); // 10
var database = {
users: ["张含韵", "江一燕", "李小璐"],
sendEmail: function (user) {
if (this.isValidUser(user)) {
console.log("你好," + user);
} else {
console.log("抱歉,"+ user +",你不是本家人");
}
},
isValidUser: function (user) {
return /^张/.test(user);
}
};
// 给每个人法邮件
database.users.forEach( // database.users中人遍历
database.sendEmail, // 发送邮件
database // 使用database代替上面标红的this
);
// 结果:
// 你好,张含韵
// 抱歉,江一燕,你不是本家人
// 抱歉,李小璐,你不是本家
[].map(function(value, index, array) {
// ...
}, [ thisObject]);
var data = [1, 2, 3, 4];
var arrayOfSquares = data.map(function (item) {
return item * item;
});
alert(arrayOfSquares); // 1, 4, 9, 16
array.filter(callback,[ thisObject]);
var arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var flag = arr3.filter(function(value, index) {
return value % 3 === 0;
});
console.log(flag); // [3, 6, 9]
注意: 空数组,则返回truearray.every(callback,[ thisObject]);
var arr4 = [1, 2, 3, 4, 5];
var flag = arr4.every(function(value, index) {
return value % 2 === 0;
});
console.log(flag); // false
注意: 空数组,则返回falsearray.some(callback,[ thisObject]);
var arr5 = [1, 2, 3, 4, 5];
var flag = arr5.some(function(value, index) {
return value % 2 === 0;
});
console.log(flag); // true
array. reduce(function(total, currentValue, currentIndex, array) {
// ...
});var arr9 = [1, 2, 3, 4];
var sum9 =arr9.reduce(function (total, curr, index, array) {
return total * curr;
});
console.log(sum9); // 24
var sum9_1 =arr9.reduce(function (total, curr, index, array) {
return total * curr;
}, 10);
console.log(sum9_1); // 240
array.reduceRight(callback,[ thisObject]);
var arr9 = [2, 45, 30, 80];
var flag = arr9.reduceRight(function (total, curr, index) {
return total - curr;
});
var flag_1 = arr9.reduceRight(function (total, curr, index) {
return total - curr;
},200);
console.log(flag); // 3
console.log(flag_1); // 43
var a = [1,2,3];
Array.isArray(a); //true
ES6数组方法
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
};
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
Array.from(arrayLike, x => x * x);
// 等同于
Array.from(arrayLike).map(x => x * x);
Array.from([1, 2, 3], (x) => x * x)
// [1, 4, 9]
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
array. copyWithin(target, start = 0, end = this.length);
// 将3号位复制到0号位
[1, 2, 3, 4, 5].copyWithin(0, 3, 4)
// [4, 2, 3, 4, 5]
// -2相当于3号位,-1相当于4号位
[1, 2, 3, 4, 5].copyWithin(0, -2, -1)
// [4, 2, 3, 4, 5]
['a', 'b', 'c'].fill(7); // [7, 7, 7]
let arr = new Array(3).fill([]);
arr[0].push(5); // [[5], [5], [5]]
find方法的回调函数可以接受三个参数,依次为当前的值、当前的位置和原数组。[1, 4, -5, 10].find((n) => n < 0)
// -5
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}) // 10
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return value > 9;
}) // 2
[1, 2, 3].includes(2) // true
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
let letter = ['a', 'b', 'c'];
let entries = letter.entries();
console.log(entries.next().value); // [0, 'a']
console.log(entries.next().value); // [1, 'b']
console.log(entries.next().value); // [2, 'c']