admin管理员组

文章数量:1356889

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.

本文标签: Javascript splitpush and joinStack Overflow