Es6 array methods


1, forEach

var colors = ['red'.'blue'.'green']

// The original traversal method
for(var a = 0; a < colors.length; a ++){console.log(colors[a]);
}


colors.forEach(function(color){})// Exercise: Iterate over the values in the number group and calculate the sum

var numbers = [1.2.3.4.5];

var sum = 0;

function adder(number){
    sum += number;
}

numbers.forEach(adder)
Copy the code

2, the map

// add the element x2 to the new array
let Arr1 = [1.2.3];

let newArr = Arr1.map((arr) = >{
    return arr * 2;
})


newArr.forEach((arr) = >{})//2. Suppose you have an array A, and the value of an attribute in array A is stored in array B

var persons = [
    {name:'zlk'.price:123},
    {name:'zlk2'.price:1231},
    {name:'zlk3'.price:12312}]let newPerson = persons.map((person) = >{
    return person.name;
})
Copy the code

3, the filter

// Demo1: Suppose we have an array object A, and place the typed objects in array A into array B

let products = [
    {name:'apple'.type:'fruit'},
    {name:'celery'.type:'vegetables'},
    {name:'cucumber'.type:'vegetables'},
    {name:'banana'.type:'fruit'}]let vegetables = products.filter((vegetable) = > {
    return vegetable.type == 'vegetables';
})




//demo2: Assuming an array A, filter out objects that do not meet the following criteria
// Condition: quantity of vegetables is greater than 0, price is less than 10
let products2 = [
    {name:'apple'.type:'fruit'.quantity:10.price:1},
    {name:'celery'.type:'vegetables'.quantity:5.price:10},
    {name:'cucumber'.type:'vegetables'.quantity:23.price:3},
    {name:'banana'.type:'fruit'.quantity:5.price:15}]let vegetables2 = products2.filter((vegetable) = > {
    return vegetable.type == 'vegetables' 
    && vegetables.quantity > 0
    && vegetables.price < 10
})



//demo3: Filters out the elements in one array based on the id of the other array
let post = {id:4.title:'es6'};

let comments = [
    {postId:4.centent:'Angular4'},
    {postId:3.centent:'Vue.js'},
    {postId:4.centent:'Node.js'},
    {postId:2.centent:'React.js'},]function commentsForPost(post,comments){
    return comments.filter((comment) = > {
        returncomment.postId == post.id; })}Copy the code

4, Once you find one, you don’t look for it again

//demo1: Assuming an array A, find the objects that match the criteria
let users = [
    {name:'zlk'.age:'12'},
    {name:'lkz'.age:'15'},
    {name:'zkl'.age:'17'},
    {name:'zlk'.age:'the'}]let user = users.find((user) = >{
    return user.name = 'zlk';
})



// Demo2: Suppose there is an array object A, find the object in the array according to the conditions of the specified object

let users2 = [
    {uId:1.name:'zlk'.age:'12'},
    {uId:2.name:'lkz'.age:'15'},
    {uId:3.name:'zkl'.age:'17'},
    {uId:4.name:'zlk'.age:'the'}]let user2 = {id:2.name:'wyf'};

function userForUsers(user2,users2){
    return users2.find((user) = >{
        return user.uId == user2.id 
    })
}
Copy the code

5, Some and Every

/** * Every one of them is false */
let every = users2.every((user) = >{
    return user.age > 17;
})



let some = users2.some((user) = >{
    return user.age > 17;
})



// Demo assumes a registration page and checks that the input value is not empty
function regest(value){
    this.value = value;
}

regest.prototype.validate=function(){
    return this.value.length > 0;
}

let username = new regest(' ');
let pwd = new regest('111222');
let phone = new regest('1201245102');

let values = [username,pwd,phone];

let checkInfo = values.every((value) = >{
    return value.validate();
})
console.log('checkInfo: ', checkInfo);

if(checkInfo){
    console.log("Registration successful!");
}else{
    console.log("Please check that the information is correct!");
}
Copy the code

6. Reduce: Can replace map, forEach, etc

//demo: calculate the sum of all numbers in the array: forEach

let sumArr = [10.20.30];
let sum2 = 0;
sumArr.forEach(arr= > {
    return  sum2 += arr;
});

console.log(sum2);

sum2 = sumArr.reduce((sum2,num) = >{
    return sum2 + num;
},0);

console.log(sum2);

//demo2: Pull one of the attributes from the array into another array: pull the map
let permaryColor = [
    {color:'red'},
    {color:'green'},
    {color:'yellow'}]let newColorArr = permaryColor.map((color) = >{
    return color.color;
})
console.log("---------map-----------");
console.log(newColorArr);
console.log("---------reduce-----------");
let newArrColor = [];
let newColorArrForReduce = permaryColor.reduce((newArrColor,oldColr) = > {
    console.log(oldColr.color);
    newArrColor.push(oldColr.color);
    return newArrColor;
},[]);

console.log(newColorArrForReduce);


//demo3: checks whether the parentheses in the string are symmetrical

let str = '() () () () () () ()';

function balancedParens(string){
    return! string.split("").reduce((pervious,char) = >{
       if(pervious < 0) {return pervious};
        if(char == "(") {return ++pervious;
        }
        if(char == ")") {return --pervious;
        }
        return pervious;
    },0);
}

console.log(balancedParens(str));

Copy the code