问题
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