JavaScript array[] 值与最后添加的值相同

发布于 2024-11-04 14:22:45 字数 1510 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2024-11-11 14:22:45

我的猜测是,因为 var object = new Object;var x = new String() 位于循环之外,因此每个对象只有一个对象,每个对象都有其在循环的后续迭代中覆盖内容(即使对象已添加到数组中,它仍然可以更改)。

尝试将这两行移动到循环中。您仍然会有一个 object JavaScript 变量(因为 JS 并不真正支持作用域级变量),但每次迭代都会有一个不同的对象。

My guess is that because var object = new Object; and var 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.

终陌 2024-11-11 14:22:45

您正在重用“object”变量,请

object = new Object();

在循环内执行此操作!

You are reusing the "object" variable, please do

object = new Object();

inside your loop!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文