After using push(), array is logged as a number

2019-01-28 23:01发布

问题:

I'm trying to get an array of some images to flip through. The first set need to be in descending order, while the second set need to be in ascending order, so I have written this:

var flipArray = [];

function createFlipArray(older, newer){

    flipArray = $("#"+older).children();

    flipArray = flipArray.get().reverse();

    flipArray = flipArray.push($('#'+newer).children());

    console.log(flipArray);


    loopThroughImages();

}

When I push the second set onto the first set, it logs the array as 4, even though there are 6 items in the whole array.

If I log the array after I populate it with the older children, it returns with HTML objects, which I expect to see after I push the newer children on.

Any suggestions?

回答1:

Array.prototype.push returns the array's new length. It modifies the original array. Remove the flipArray = before it.



回答2:

.push modifies the array in-place. It does not return a new array, it returns the array's new length.