ES6 Basics 2

The method called by the constructor: static method

Methods invoked by instance objects: instance methods

1. New method of array object (supplement)

1.1  Array.isArray(arr)       true / false

console.log(Array.isArray([1,2,3]))

1.2 filter Filter array elements, if the return value is true, put the elements into a new array

       map sorts the array and puts the computed result into a new array

<script src="cities.js"></script>
<script>
 1. 将城市名字中包含 白  字的 输出
     let arr = cities.filter(city => city.nm.includes('白'))

 2. id末尾为0的城市
     let arr = cities.filter(city => city.id % 10 === 0);
     console.log(arr);

 3. 将拼音大写
     let arr = cities.map(city => {
         city.py = city.py.toUpperCase();
         return city;
     })

 4. 将数组的值全部返回3次方
     let arr = [1,3,5,7,9];
     let newArr = arr.map(n => n**3);
     console.log(newArr);
</script>

 1.3 some(callback) Iterates through the array, as long as one of the conditions is met, it returns true and terminates the traversal, otherwise it returns false

<script src="cities.js"></script>
<script>
 遇到第一个含有 石 字的城市停下来
     let n = cities.some(city => {city.nm.includes('石'));
     console.log(n);
</script>

1.4 every(callback) traverses the array, as long as one of the conditions is not met, it returns false and terminates the traversal, otherwise it returns true

1.5 forEach(callback) traverses the array without return value

1.6  reduce(function(preValue,nowValue,index,arrSelf) {return preValue + nowValue})

       accumulation array unit

       preValue previous value

       nowValue current value

       index current value subscript

       arrSelf the current array

<script>  
求和
     let arr = [1,2,3,4];
     let sum = arr.reduce((preValue, nowValue) => preValue+nowValue)
     console.log(sum)
</script>

2. New features of objects

2.1 Concise representation of attributes and methods (the key name is the same as the value name)

<script>
 var name = "王公子";
    var skill = "今夜买单";
    var hobby = "唱跳";
    var wgz = {
         name: name,
         skill: skill,
         hobby: hobby
    }
    let wgz = {
        name,skill, hobby,
        sing() {},
        jump() {},
        run() {}
    }
</script>

2.2 Object.is() is equivalent to congruent but different

       Object.is(1,2)

       Object.is(NaN,NaN)

       Object.is(0,-0)

<script> 
if (NaN === NaN) {
     console.log("ok");
 }
 if (Object.is(NaN, NaN)) {
      console.log('yes');
 }
</script>

2.3 Object.assign(obj1, obj2, obj3) merge objects 

2.4 Object.keys(obj) returns the key value of obj to an array

        Object.values(obj) returns the value of obj to an array

<script> 
let cc = {
        A: [],
        B:[],
        D:[],
        T: []
    }
 let arr = Object.keys(cc);
 console.log(arr);
</script>

3.set data structure

3.1 Similar to an array, but with unique members and no index

3.2 var set = new Set(x) x: array, array-like, string

3.3 The length of the set is not length but size

3.4  方法 : add(x);delete(x);has(x);clear(x)

3.5 You can use for of and forEach to traverse the set

// 特征:成员唯一
function MySet(array=[]) {
    let arr = [];
    array.forEach(item => {
        if (!arr.includes(item)) {
            arr.push(item)
        }
    });
    this.array = arr;
} 

// 增加:add
MySet.prototype.add = function(item) {
    this.array.includes(item) || this.array.push(item);
}

// 删除:del
MySet.prototype.del = function(item) {
    let index = this.array.indexOf(item);
    if (index === -1) {
        return;
    }
    this.array.splice(index, 1);
}

// 清空:clear
MySet.prototype.clear = function() {
    this.array = [];
}

// 是否存在:has
MySet.prototype.has = function(item) {
    return this.array.includes(item);
}

    数组去重
    Array.prototype.singleArr = function() {
        return [...new Set(this)];
    } 

Guess you like

Origin blog.csdn.net/Cc200171/article/details/125284694
Recommended