JavaScript array[] 值与最后添加的值相同
forEach 中的警报会打印插入的值,但在 forEach 之后,数组中的所有值都是相同的。假设我成功插入了 3 个值。我在代码中的某处有一个全局数组[],我尝试在函数中更改它,但结果相同,JSTL 可能会出现问题?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
var jokeArray = [];
function ALL(){
var object = new Object;
var x = new String();
<c:forEach var="joke" items="${jokeAllList}">
x = '${joke.content}';
x = x.replace(/{/g,"\n");
x = x.replace(/}/g,"\r");
object.content = x;
object.category = '${joke.category}';
object.rate = '${joke.rate}';
object.postby = '${joke.postby}';
object.votes = '${joke.votes}';
object.image = '${joke.image}';
jokeArray.unshift(object);
alert(jokeArray[0].content);
</c:forEach>
why index(s) 0 & 1 & 2 have the same VALUES ? ( Assume there are values and jokeArray.length = 3 )
alert ( "arr length is " + jokeArray.length);
alert(jokeArray[0].content);
alert(jokeArray[1].content);
alert(jokeArray[2].content);
alert("done ALL method");
};
alert("done ALL method");
已发出警报,因此没有错误
the alert within the forEach prints the values as they are inserted , but after the forEach all the values in the array are the same. Assume I have 3 values successfully insererted . I have a global array[] somewhere up in the code , I tried change it within the function , but same result , JSTL making a a problem mayb?
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
var jokeArray = [];
function ALL(){
var object = new Object;
var x = new String();
<c:forEach var="joke" items="${jokeAllList}">
x = '${joke.content}';
x = x.replace(/{/g,"\n");
x = x.replace(/}/g,"\r");
object.content = x;
object.category = '${joke.category}';
object.rate = '${joke.rate}';
object.postby = '${joke.postby}';
object.votes = '${joke.votes}';
object.image = '${joke.image}';
jokeArray.unshift(object);
alert(jokeArray[0].content);
</c:forEach>
why index(s) 0 & 1 & 2 have the same VALUES ? ( Assume there are values and jokeArray.length = 3 )
alert ( "arr length is " + jokeArray.length);
alert(jokeArray[0].content);
alert(jokeArray[1].content);
alert(jokeArray[2].content);
alert("done ALL method");
};
alert("done ALL method");
is alerted , so no errors
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,因为
var object = new Object;
和var x = new String()
位于循环之外,因此每个对象只有一个对象,每个对象都有其在循环的后续迭代中覆盖内容(即使对象已添加到数组中,它仍然可以更改)。尝试将这两行移动到循环中。您仍然会有一个
object
JavaScript 变量(因为 JS 并不真正支持作用域级变量),但每次迭代都会有一个不同的对象。My guess is that because
var object = new Object;
andvar x = new String()
are outside the loop, you only have a single object for each which is having its contents overwritten on subsequent iterations of the loop (even though the object has been added to the array it can still be changed).Try moving those two lines into the loop. You'll still have a single
object
JavaScript variable (because of the way JS doesn't really support scope-level variables) but you will have a different object for each iteration.您正在重用“object”变量,请
在循环内执行此操作!
You are reusing the "object" variable, please do
inside your loop!