将新数组推入二维数组 [英] pushing new array into a 2d array

查看:42
本文介绍了将新数组推入二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试将新数组推入全局数组:


I'm trying to push a new array into a global array here:

var history = [ ];

function addHistory(array)
{
history.push(array);
console.log(history)//to check what is the value of array
}

var test1 = [0,0,0,0,0];
var test2 = [1,1,1,1,1];
addHistory(test1);
addHistory(test2);

这样做之后,数组应该是:

After doing like this, the array should be:

[ [0,0,0,0,0] , [1,1,1,1,1] ]

但是它会打印

[ [1,1,1,1,1] , [1,1,1,1,1] ]

因此,基本上,它替换了数组"history"中的所有旧数组,而不是将其推到末尾.

So basically it replaces all old arrays in array "history", instead of pushing it at the end.

这有什么问题吗?
非常感谢

What can be wrong here?
Thanks a lot


抱歉,忘记提及我的实际变量不称为历史记录(我这样称呼它只是为了您可以想象我想要的).它称为" rollHist "

推荐答案

history 是javascript中的受保护术语.将其更改为此将对其进行修复:

history is a protected term in javascript. Changing it to this will fix it:

var myHistory = [];

function addHistory(array)
{
myHistory.push(array);
console.log(myHistory)//to check what is the value of array
}

var test1 = [0,0,0,0,0];
var test2 = [1,1,1,1,1];
addHistory(test1);
addHistory(test2);

您可以在此处

这篇关于将新数组推入二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆