当前位置:网站首页>New attributes of ES6 array
New attributes of ES6 array
2022-07-27 17:01:00 【sl105105】
forEach() Traversal array
Perform the given function once for each element of the array . Can replace for Loop through each array , There are three parameters , Each current value of the first parameter array , The current index of the second array , The third is the array object itself .
forEach loop , There is no return value at the end of the loop , The return value is undefined.
a key :forEach Loop callback function , Can not be return Give a value , But you can use return To terminate the execution of the method
var arr = [1, 2, 3, 4, 5];
arr.forEach(function (itme) {
console.log(arr);
})map() Method returns a new array according to the callback function , This new array consists of the return value of each element in the original array after calling the provided function once .

Back base (base) The index of (exponent) The next power , namely base^exponent
var arr = [1, 2, 3, 4, 5];
var res = arr.map(function (itme) {
// The first use Math.pow()
// return Math.pow(itme, 2)
// The second use Es6 Methods ** The next power
return itme ** 2
})
console.log(res); filter() Filter array , As above, there are three parameters , The first parameter is each element , The second parameter is the subscript , The third parameter is the original array , But he returns all the values that meet the requirements , Will find everything , adopt return Judgment condition , The judgment condition is true Form a new array and return it
var res = person.filter(function (itme, index, array) {
return itme.age > 18
})
console.log(res);
// practice : There are the following arrays , ask : Who are the good people ? What are the bad guys ?
var acts = [{
name: " Big snake pill ",
isGood: false
},
{
name: " Kakasi ",
isGood: true
},
{
name: " Yu Zhibo ",
isGood: false
},
{
name: " Naruto ",
isGood: true
}]
var res = acts.filter(function (itme) {
// Be careful isGood Itself is a boolean type , So there's no need to ==true
// A good man
// return itme.isGood
// The bad guys
return !itme.isGood
})
console.log(res) find() The method has three arguments , The first parameter is each element , The second parameter is the subscript , The third parameter is the original array , It returns the first qualified object when the condition is true , Find the first one and don't look .
adopt return The condition of returns a New objects
function fn(person) {
return person.age > 20
}
console.log(person.find(fn)); The second way
var res = person.find(function (itme, index, array) {
return itme.age > 20
})
console.log(res);findindex() Circular array , Method returns the... Of the first element of the array that satisfies the provided test function Indexes . If no corresponding element is found -1.
Returns the index of the first matching condition element in the array , When you find it, you don't look for it . If there is no match, return -1;

var arr = [1, 2, 3, 4, 5];
var str = arr.findIndex(function (itme) {
return itme > 3
})
console.log(str);every() Method to test the... In an array Whether all elements can pass the test of a specified function . It returns a Boolean value , All satisfied as true, One dissatisfaction is false
Circular array , adopt return To judge the conditions , If each item of the array meets the condition, return ture, There is an unsatisfied return false.
notes : It can be used as a judgment condition

var res = person.every(function (itme) {
return itme.age >= 18
})
console.log(res ? " All meet " : " There is one unsatisfied ");some() Test array , test Is there at least 1 Elements passed the provided function test . If you find one, don't look , What it returns is a Boolean Type value .
Circular array , adopt return To judge the conditions , If one of the array meets the condition, return ture, If you are not satisfied with everything, return false.

var res = person.some(function (itme) {
return itme.age < 18
})
console.log(res ? " Yes " : " nothing ");reduce() Higher order function , There are four parameters : accumulator acc , Current value cur( No value given , Default acc by 0), The accumulation in the array can be processed , Generally, multiply and add are used more .
It can realize the accumulation and summation of arrays

var arr = [1, 2, 3, 4, 5];
var res = arr.reduce(function (acc, cur) {
return acc * cur
// return acc + cur
}, 10)
console.log(res);math.pow() Function returns to
toString() This method on the array is to convert the array into a string , The visual effect is to remove brackets ( No matter how many parentheses can be removed ), It doesn't change the original array , What is returned is the changed element
Array , There are both strings and numbers toString() Method , Pay attention to array usage toString() You need to use variable methods
var arr = [6, 22, 3, 1, 55]
console.log(arr.toString()); Method of converting class array to true array :
First of all, let's understand why we need to turn to arrays , Class array is not a real array, so you can't use array methods , So we need to convert it to a true array .
Which class arrays are there ?arguments Is the keyword in the method , Used to store actual parameters
// Pseudo array : Only length attribute
// The first one is :argumentes
function add(a, b) {
console.log(arguments);
}
add(1, 2, 3, 4, 5)
// The second kind : character string
var str="abcd"
console.log(str[0]);
// The third kind of :DOM Trees
var dom=document.getElementsByTagName("p")
console.log(dom);ES5 The true array uses Array.prototype.slice.call

ES6 There are two kinds of true array :
The first one is :Array.from()
function add(a, b) {
var arr = Array.from(arguments)
console.log(arr);
}
add(1, 2, 3, 4, 5) Method 2 : utilize ... Expand operator implementation
function add(a, b) {
var arr = [...arguments]
console.log(arr);
}
add(1, 2, 3, 4, 5) How to determine whether an array can be used stay ES6 Provides Array.isArray(), If you return true Is the real array , conversely false It's not

And in the ES5 It's using instanceof To determine whether it is a true array , But there are some limitations , There are limitations in the case of global variables .

Than instanceof A better way to use it is Obj.prototype.toString.call()

or Judge him indexOf Is it greater than -1, To determine whether it is a true array

Array.of() Create array
Obiect.keys() Get all the properties of the object itself , similar fo...in, The return value is an array .

边栏推荐
- Natural sorting: comparable interface, customized sorting: the difference between comparator interface
- If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
- Polynomial locus of order 5
- Structure and bit segment of C language
- JSON data parsing
- JDBC connection database
- kubesphere多节点安装出错
- md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
- ES6数组的方法及伪数组转数组方法
- Random number formula random
猜你喜欢
随机推荐
Circular statements and arrays
牛客题目——最小的K个数
High precision timer
Get the array list of the previous n days and the previous and subsequent days of the current time, and cycle through each day
MySQL—连表查询
合工大苍穹战队视觉组培训Day8——视觉,目标识别
Log4j.jar and slf4-log4 download link
京东张政:内容理解在广告场景下的实践和探索
json数据解析
MPC5744p时钟模块
Start from scratch blazor server (1) -- project construction
JSON data parsing
两表联查1
Operators of C language
ShardingSphere-proxy-5.0.0分布式雪花ID生成(三)
Servlet uses cookies to realize the last login time of users
Servlet Chinese garbled setcontenttype setting is invalid
kubesphere多节点安装出错
What is JSP?
第7天总结&作业









