当前位置:网站首页>Use of ES6 array
Use of ES6 array
2022-04-23 05:24:00 【Your beauty fascinates me】
// // Convert parameters to arrays
console.log(Array.of(1,3,4,5,2));//[1,3,4,5,2]
//Array.from()
// Converts a class array object or iteratable object to an array .
// Parameters are arrays , Return the same array as the original array
console.log(Array.from([1, 2])); // [1, 2]
// Parameter contains vacancy
console.log(Array.from([1, , 3])); // [1, undefined, 3]
//mapFn Optional ,map function , Used to process each element , Into the array are the processed elements .
console.log(Array.from([1, 2, 3], (n) => n * 2)); // [2, 4, 6]
// thisArg Optional , Is used to specify the map Function execution time this object .
let map = {
do: function(n) {
return n * 2;
}
}
let arrayLike = [1, 2, 3];
console.log(Array.from(arrayLike, function (n){
return this.do(n);
}, map)); // [2, 4, 6]
// Class array object
// A class array object must contain length attribute , And the element attribute name must be a numeric value or a character that can be converted to a numeric value .
let arr = Array.from({
0: ' I'm not digital ',
1: '2',
2: 3,
length: 3
});
console.log(arr[0]); // ['1', '2', 3]
// No, length attribute , Then return an empty array
let array = Array.from({
0: '1',
1: '2',
2: 3,
});
console.log(array); // []
// The element attribute name is not numeric and cannot be converted to numeric , Return length is length The element value is undefined Array of
let array1 = Array.from({
a: 1,
b: 2,
length: 2
});
console.log(array1); // [undefined, undefined]
// Convert iteratable objects
// transformation map
let map2 = new Map();
map2.set('key0', 'value0');
map2.set('key1', 'value1');
console.log(Array.from(map2)); // [['key0', 'value0'],['key1',
// 'value1']]
// transformation set
let arr2 = [1, 2, 3];
let set = new Set(arr2);
console.log(Array.from(set)); // [1, 2, 3]
// Convert string
let str3 = 'abc';
console.log(Array.from(str3)); // ["a", "b", "c"]
// find()
// Find the elements in the array that match the criteria , If there are more than one element that meets the criteria , Then return the first element .
let arr4 = Array.of(1, 2, 3, 4);
console.log(arr4.find(item => item > 2)); // 3
// Array space is treated as undefined
console.log([, 1].find(n => true)); // undefined
// findIndex()
// Find the index of the eligible elements in the array , If there are more than one element that meets the criteria , Returns the first element index .
let arr5 = Array.of(1, 2, 1, 3);
// Parameters 1: Callback function
// Parameters 2( Optional ): Specifies... In the callback function this value
console.log(arr5.findIndex(item => item == 2)); // 1
// Array space is treated as undefined
console.log([, 1].findIndex(n => true)); //0
// fill()
// Fills the contents of array elements with a range of indexes into a single specified value .
let arr6 = Array.of(1, 2, 3, 4);
// Parameters 1: The value to fill
// Parameters 2: The starting index to be filled
// Parameters 3( Optional ): Filled end index , The default is the end of the array
console.log(arr6.fill(0,1,2)); // [1, 0, 3, 4]
// copyWithin()
// Modify an array element with a certain range index to another element with a specified range index in this array .
// Parameters 1: The modified starting index
// Parameters 2: The starting index of the data to be overwritten
// Parameters 3( Optional ): The end index of the data to be overwritten , The default is the end of the array
console.log([1, 2, 3, 4].copyWithin(0,1,3)); // [2,3,4,4]
// Parameters 1 A negative number means the reciprocal
console.log([1, 2, 3, 4].copyWithin(-2, 0)); // [1, 2, 1, 2]
console.log([1, 2, ,4].copyWithin(0, 2, 4)); // [, 4, , 4]
// entries()
// Traversal key value pairs .
for(let [key, value] of ['a', 'b'].entries()){
console.log(key, value);
}
// 0 "a"
// 1 "b"
// Don't use for... of loop
let entries = ['a', 'b'].entries();
console.log(entries.next().value); // [0, "a"]
console.log(entries.next().value); // [1, "b"]
// The array contains empty bits
console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]
// keys()
// Traverse the key name .
for(let key of ['a', 'b'].keys()){
console.log(key);
}
// 0
// 1
// The array contains empty bits
console.log([...[,'a'].keys()]); // [0, 1]
// values()
// Traverse the key values .
for(let value of ['a', 'b'].values()){
console.log(value);
}
// "a"
// "b"
// The array contains empty bits
console.log([...[,'a'].values()]); // [undefined, "a"]
// includes()
// Whether the array contains the specified value .
// Be careful : And Set and Map Of has Method of differentiation ;Set Of has Method is used to find the value ;Map Of has Method is used to find the key name .
// Parameters 1: Contains the specified value of
console.log([1, 2, 3].includes(1)); // true
// Parameters 2: Optional , The starting index of the search , The default is 0
console.log([1, 2, 3].includes(1, 2)); // false
// NaN Including judgment
console.log([1, NaN, 3].includes(NaN)); // true
// Nested array to one-dimensional array
// flat()
console.log([1 ,[2, 3]].flat()); // [1, 2, 3]
// Specifies the number of nesting levels for the transformation
console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]]
// No matter how many layers are nested
console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
// Automatically skip empty space
console.log([1, [2, , 3]].flat()); // [1, 2, 3]
// Copy the array
let arr7 = [1, 2],
arr8 = [...arr7];
console.log(arr8); // [1, 2]
// The array contains empty bits
let arr9 = [1, , 3],
arr10 = [...arr9];
console.log(arr10); //[1, undefined, 3]
// Merge array
console.log([...[1, 2],...[3, 4]]); // [1, 2, 3, 4]
effect :

版权声明
本文为[Your beauty fascinates me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230521367926.html
边栏推荐
- Interpretation of common SQL statements
- C language hash dictionary and notes
- 2021-10-08
- Implementation of resnet-34 CNN with kears
- Devops life cycle, all you want to know is here!
- 使用宝塔+xdebug+vscode远程调试代码
- 狼叔来找翻译人员了--plato--持续翻译中.....
- Multi process model in egg -- egg document Porter
- Tslint annotations ignore errors and restful understanding
- Redis的基本知识
猜你喜欢

What are the reasons for the failure of digital transformation?

After NPM was upgraded, there was a lot of panic

Laravel routing settings

《2021年IT行业项目管理调查报告》重磅发布!

Blender programmed terrain production

云计算与云原生 — OpenShift 的架构设计

7-4 is it too fat (10 points) PTA

Basic use of sqlyog

SQL Server检索SQL和用户信息的需求

Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
随机推荐
The introduction of lean management needs to achieve these nine points in advance
Using PHP post temporary file mechanism to upload arbitrary files
My old programmer's perception of the dangers and opportunities of the times?
MFC实现资源单独Dll实现
d.ts---更详细的知识还是需要看官网的介绍(声明文件章节)
MFC implementation resources are implemented separately by DLL
Domain driven model DDD (III) -- using saga to manage transactions
To understand Devops, you must read these ten books!
SQLyog的基本使用
What are the most popular recruitment technical skills in 2022? You can't think of it
Graphics. Fromimage reports an error "graphics object cannot be created from an image that has an indexed pixel..."
The source of anxiety of graduating college students looking for technology development jobs
Devops life cycle, all you want to know is here!
Ehcache Memcache redis three caches
2021-11-08
引入精益管理方式,需要提前做到这九点
Study notes: unity customsrp-13-colorgrading
我这位老程序员对时代危险和机遇的一点感悟?
使用 Kears 实现ResNet-34 CNN
分支与循环语句