Javascript split, push and join

て烟熏妆下的殇ゞ 提交于 2019-12-02 16:06:09

问题


How come the | is not added when I call the join method

    var array ="12|23|435|566|46|6|666766|24";
    var arraySplit = array.split("|");
    var newArray = [];
    for (i=0; i<arraySplit.length; i++)
    {
        if (arraySplit[i] < 500)
        {
            newArray.push(arraySplit[i]);
        }
    }
    newArray.join("|");
    alert(newArray);

回答1:


newArray.join does not modify the existing array. It returns a new string of all the array's current values, joined by the string you specify. Use the following to store the generated array in a new variable:

var joinedArray = newArray.join("|");
alert(joinedArray);

DEMO: http://jsfiddle.net/EH8dB/


References:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join



回答2:


You are not modifying newArray since join() returns a new object.



来源:https://stackoverflow.com/questions/12868756/javascript-split-push-and-join

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