// 父类 class People/*(理解为ESC5构造函数)*/ { constructor(name, age) { // 属性 this.name = name this.age = age } // 方法与方法之间无逗号,分号 eat() { alert(`${this.name} eat something`) } speak() { alert(`My name is ${this.name},age ${this.age}`) } }; // 子类,子级想调用父级的方法用 super class Student extends People { constructor(name, age, number) { super(name, age) this.number = number } study() { alert(`${this.name} study`) } } let zhang = new People('zhang', 20) zhang.eat() let xiaohong = new Student('xiaohong',11 , '学号'); xiaohong.study()
filter
// 过滤不满足条件的对象, 也可以根据 A中id=== B中id来过滤出不符合的 let xin = [ { id: 1, name: "老大" }, { id: 3, name: "老er" }, { id: 6, name: "老san" }, { id: 4, name: "老liu" } ]; xin = xin.filter(a => { return a.id > 1 && a.id < 5 }) console.log(xin); // 移除指定相等的值,返回新数组 let arr = [1, 2, 3] function remove(arr, item) { return arr.filter(ele => { return ele != item; }) } console.log(remove(arr, 1)); // 获取数组指定类型对象放到新数组 let products = [ { name: "cucumber", type: "vegetable" }, { name: "banana", type: "fruit" }, { name: "celery", type: "vegetable" }, { name: "orange", type: "fruit" } ]; let products2 = products.filter(product => { return product.type === 'fruit'; }) console.log(products2);
readuce // 求和 let arr = [0, 20, 30]; arr.reduce(function (a, b) { return a += b }, 0) // 把数组对象值传到新数组 var primaryColors = [ { color: "red" }, { color: "yellow" }, { color: "blue" } ]; var colors = primaryColors.reduce((prevIoUs, primaryColors) => { prevIoUs.push(primaryColors.color); return prevIoUs; }, []) console.log(colors);
map const array1 = [1, 4, 9, 16]; const map1 = array1.map(x => x * 2); console.log(map1); // Array [2, 8, 18, 32] // 将对象数组 某个属性值存到 另一个 let cars = [ {model:"buick", price:"CHEAP"}, {model:"BMW",price:"exp"} ] let prices = cars.map(function(car){ return car.price })
set
// 去除数组的重复成员 [...new Set(array)] let arr = [1,1,2,2,3,3,1]; Array.from([...new Set(arr)]) // 字符串去重, 新数组 let arr = [...new Set('ababbc')].join('') console.log(Array.from(arr));