最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript split, push and join - Stack Overflow

programmeradmin1浏览0评论

How e 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);

How e 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);
Share Improve this question asked Oct 13, 2012 at 0:14 mporampora 1,4795 gold badges26 silver badges67 bronze badges 1
  • Have a look at MDN's documentation: developer.mozilla/en-US/docs/JavaScript/Reference/… – Felix Kling Commented Oct 13, 2012 at 0:38
Add a ment  | 

2 Answers 2

Reset to default 7

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/EH8dB/


References:

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

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

发布评论

评论列表(0)

  1. 暂无评论