刷题-6

308 阅读1分钟

刷题-6 Array

1、题目


const a = [1, 2, 3, 4, 5]
a.multiply()
console.log(a) // [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]

答案


// 在 Array 原型上增加 multiply 方法
Array.prototype.multiply = function () {
    this.push(...(this.map(v => v * v)))
}
a.multiply()
console.log(a) // [1, 2, 3, 4, 5, 1, 4, 9, 16, 25]