function cast () {
return Array.prototype.slice.call(arguments);
}
cast('a','b','c','d'); // ["a", "b", "c", "d"]
function cast () {
return [].slice.call(arguments);
}
cast('a','b','c','d'); // ["a", "b", "c", "d"]
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
}
console.log(Array.from(arrayLike)); // ["a","b","c"]
function cast (){
return [...arguments]
}
cast('a','b','c'); // ["a","b","c"]
let arr = Array.from('w3cplus.com')
console.log(arr); // ["w","3","c","p","l","u","s",".","c","o","m"]
let namesSet = new Set(['a', 'b'])
let arr2 = Array.from(namesSet)
console.log(arr2); //["a","b"]
let arr = Array.from([1, 2, 3]); console.log(arr); // [1,2,3]
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.from({ length: 2 }, () => 'jack')
// ['jack', 'jack']
function countSymbols(string) {
return Array.from(string).length;
}
function typesOf () {
return Array.from(arguments, value => typeof value)
}
typesOf(null, [], NaN)
// <- ['object', 'object', 'number']
function typesOf (...all) {
return all.map(value => typeof value)
}
typesOf(null, [], NaN)
// <- ['object', 'object', 'number']
Array.of
Array.of = function of () {
return Array.prototype.slice.call(arguments)
}
Array.prototype.slice.call([1, 2, 3]) // <- [1, 2, 3] Array.of(1, 2, 3) // <- [1, 2, 3] Array.of(3) // <- [1]
new Array() // <- [] new Array(undefined) // <- [undefined] new Array(1) // <- [undefined x 1] new Array(3) // <- [undefined x 3] new Array(1, 2) // <- [1, 2] new Array(-1) // <- RangeError: Invalid array length
Array.of() // <- [] Array.of(undefined) // <- [undefined] Array.of(1) // <- [1] Array.of(3) // <- [3] Array.of(1, 2) // <- [1, 2] Array.of(-1) // <- [-1]
function ArrayOf(){
return [].slice.call(arguments);
}
Array.prototype.copyWithin(target, start = 0, end = this.length)
var items = [1, 2, 3, ,,,,,,,]; // <- [1, 2, 3, undefined x 7]
items.copyWithin(6, 1, 3) // <- [1, 2, 3, undefined × 3, 2, 3, undefined × 2]
// 将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]
// 将3号位复制到0号位
[].copyWithin.call({length: 5, 3: 1}, 0, 3)
// {0: 1, 3: 1, length: 5}
// 将2号位到数组结束,复制到0号位
var i32a = new Int32Array([1, 2, 3, 4, 5]);
i32a.copyWithin(0, 2);
// Int32Array [3, 4, 5, 4, 5]
// 对于没有部署TypedArray的copyWithin方法的平台
// 需要采用下面的写法
[].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4);
// Int32Array [4, 2, 3, 4, 5]
Array.prototype.fill
['a', 'b', 'c'].fill(0) // <- [0, 0, 0] new Array(3).fill(0) // <- [0, 0, 0]
['a', 'b', 'c',,,].fill(0, 2) // <- ['a', 'b', 0, 0, 0] new Array(5).fill(0, 0, 3) // <- [0, 0, 0, undefined x 2]
new Array(3).fill({})
// <- [{}, {}, {}]
new Array(3).fill(function foo () {})
// <- [function foo () {}, function foo () {}, function foo () {}]
Array.prototype.find
[1, 2, 3, 4, 5].find(item => item > 2) // <- 3 [1, 2, 3, 4, 5].find((item, i) => i === 3) // <- 4 [1, 2, 3, 4, 5].find(item => item === Infinity) // <- undefined
[1, 5, 10, 15].find(function(value, index, arr) {
return value > 9;
}) // 10
Array.prototype.findIndex
[1, 2, 3, 4, 5].find(item => item > 2) // <- 2 [1, 2, 3, 4, 5].find((item, i) => i === 3) // <- 3 [1, 2, 3, 4, 5].find(item => item === Infinity) // <- -1
[NaN].indexOf(NaN) // -1 [NaN].findIndex(y => Object.is(NaN, y)) // 0
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']
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有