.push() multiple objects into JavaScript array returns 'undefined'

前端 未结 3 1383
慢半拍i
慢半拍i 2021-02-07 04:54

When I add items to the beats array and then console.log the User, I\'m getting the correct number of items in the array. But when I check .length, I always get 1. Trying to ca

3条回答
  •  离开以前
    2021-02-07 04:58

    Spread operator

    In environments that support the spread operator you may now do the following:

    this.addBeats = function (beats) {
        return this.beats.push(...beats);
    };
    

    Or if you need more control for overwriting etc

    this.addBeats = function(beats) { 
        return this.beats.splice(this.beats.length, null, ...beats);
    };
    

提交回复
热议问题