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

主宰稳场 提交于 2020-01-09 11:56:54

问题


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.



来源:https://stackoverflow.com/questions/15165288/after-using-push-array-is-logged-as-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!