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

2019-01-28 23:31发布

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?

2条回答
等我变得足够好
2楼-- · 2019-01-28 23:43

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

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-29 00:06

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

查看更多
登录 后发表回答