当前位置:网站首页>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
边栏推荐
- What are the most popular recruitment technical skills in 2022? You can't think of it
- Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
- 使用宝塔+xdebug+vscode远程调试代码
- Laravel database
- Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
- 如果我是pm之 演出电影vr购票展示
- Power consumption parameters of Jinbei household mute box series
- Implementation of resnet-34 CNN with kears
- Traversal of tree
- 2022年最热门的招聘技术技能是什么,您绝对想不到
猜你喜欢
Excel 2016 打开文件第一次打不开,有时空白,有时很慢要打开第二次才行
phphphphphphphp
Minimum spanning tree -- unblocked project hdu1863
看板快速启动指南
Flip coin (Blue Bridge Cup)
Requirements for SQL server to retrieve SQL and user information
PIP free export with path (@ file: / / /) notes
Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”
Laravel routing settings
How to add beautiful code blocks in word | a very complete method to sort out and compare
随机推荐
MySQL series - install MySQL 5.6.27 on Linux and solve common problems
WTL self drawn control library (cqscheckcomboxbox)
日志简介和构建web应用
数字化转型失败,有哪些原因?
Uncle wolf is looking for a translator -- Plato -- ongoing translation
源码剖析Redis中如何使用跳表的
双击.jar包无法运行解决方法
Output string in reverse order
Study notes: unity customsrp-12-hdr
npm升级后问题,慌得一批
The prefix of static of egg can be modified, including boots
Interview summary
If the route reports an error after deployment according to the framework project
Domain driven model DDD (III) -- using saga to manage transactions
Three of three JS (webgl) is simple to draw lines / arcs according to points (based on linegeometry / line2 / linematerial, draw two arc segments based on the center of the circle)
Modèle axé sur le domaine DDD (III) - gestion des transactions à l'aide de Saga
2021-09-27
Error handling mechanism of the strongest egg framework in history
分支与循环语句
App Store年交易额100万美元只缴15%佣金,中小开发者心里很矛盾