{
let arr = Array.of(3, 4, 7, 9, 11);
console.log('arr', arr); //[3,4,7,9,11]
let empty = Array.of();
console.log(empty); //[]
}
//ES6为Array增加了from函数用来将其他对象转换成数组。
//当然,其他对象也是有要求,也不是所有的,可以将两种对象转换成数组。
//1.部署了Iterator接口的对象,比如:Set,Map,Array。
//2.类数组对象,什么叫类数组对象,就是一个对象必须有length属性,没有length,转出来的就是空数组。
{
//转换数组
let p = document.querySelectorAll('p');
let pArr = Array.from(p);
pArr.forEach(function(item) {
console.log(item.textContent); //es6,嘿嘿嘿,快乐动起来
});
//map
console.log(Array.from([1, 3, 5], function(item) {
return item * 2
})) //2,6,10
}
//数组填充
{
console.log('fill-7', [1, 'a', undefined].fill(7)) // 7,7,7
console.log('fll,pos', ['a', 'b', 'c'].fill('7', 1, 3)); //a 7 7 (1,3)表示起始位置,7为填充内容
}
//遍历相关
{
//取位置
for (let index of['1', 'c', 'ks'].keys()) {
console.log('keys', index); //0 1 2
}
//取值
for (let value of['1', 'c', 'ks'].values()) {
console.log('values', value); //1 c ks (这里有兼容问题,需引入 import 'babel-polyfill')
}
//取位置和值
for (let [index, value] of['1', 'c', 'ks'].entries()) {
console.log('values', index, value); //0 1 2,1 c ks
}
}
//在当前数组内部数组内一个成员赋值到其他地方
{
console.log([1, 2, 3, 4, 5].copyWithin(0, 3, 4)); //4,2,3,4,5 (开始位置0,从第三个开始读起,截止位置为4,即5个数)
console.log([1, 2, 3, 4, 5].copyWithin(1, 3, 4)) //1,4,3,4,5
console.log([1, 2, 3, 4, 5].copyWithin(0, -2, -1)) //4,2,3,4,5
}
//查找
{
console.log([1, 2, 3, 4, 5, 6].find(function(item) {
return item > 3 //4 只找第一个值,不往后了
}));
console.log([1, 2, 3, 4, 5, 6].findIndex(function(item) {
return item > 3 //3 找的是下标
}));
}
//NaN
{
console.log('number',[1,2,NaN].includes(1)); // true 是否包含这个值
console.log('number',[1,2,NaN].includes(NaN)); // true
}