如何在javascript中将相同的元素插入到数组中?
我有一系列对象。但是当我插入之前添加的对象时,它将覆盖我之前的对象。我该如何解决它?
我有一个叫做玩家的对象。在播放器中,我有两个数组:一个名为 onHandWeapon,一个名为 onFieldWeapon。它们是武器对象的数组。
function player(lp){
this.lp = lp;
this.onFieldWeapon = new Array();
this.onHandWeapon = new Array();
}
function weapon(id, heart, bullet, src){
this.id = id;
this.heart = heart;
this.bullet = bullet;
this.src = src;
this.location;
this.name;
this.discription;
this.bufferBullet = bullet;
}
我在 onHandWeapon 数组中设置了三个虚拟对象。然后我想随机拿起其中一个并将其放入 onFieldWeapon 并为其分配一个随机位置。
function aiCreateWeapon(){
var b = Math.floor(Math.random()*ai.onHandWeapon.length);
$('#console').append(' ' + b + ' ');
var ip = 100;
while($('#'+ip).attr('class') != 'enemyField'){
ip = Math.floor(Math.random()*48);
}
encurrentWeapon = ai.onHandWeapon[b];
var source = encurrentWeapon.src;
var oImg = document.createElement("img");
oImg.setAttribute('src', source);
oImg.setAttribute('height', '60px');
oImg.setAttribute('width', '60px');
$('#'+ip).append(oImg).show('explode','slow');
encurrentWeapon.location = ip;
ai.onFieldWeapon.push( encurrentWeapon);
$('#console').append(' ' + ai.onFieldWeapon[0].location + ' ');
}
aiCreateWeapon 是绑定到按钮的函数。当我单击它时,ai.onFieldWeapon[0].location 是一个固定位置,直到它发生变化。我检查过每次将与第一个元素相同的对象添加到 onFieldWeapon 数组时,它都会覆盖第一个元素的数据。
I have a array of objects. But when I insert the object the I previously added, it will override my previous object. how can I solve it?
I have on object called player. In player, I have two array : one called onHandWeapon, one called onFieldWeapon. They are array of weapon object.
function player(lp){
this.lp = lp;
this.onFieldWeapon = new Array();
this.onHandWeapon = new Array();
}
function weapon(id, heart, bullet, src){
this.id = id;
this.heart = heart;
this.bullet = bullet;
this.src = src;
this.location;
this.name;
this.discription;
this.bufferBullet = bullet;
}
I have set three dummy object in onHandWeapon array. Then I want to randomly pick up one of them and put it into onFieldWeapon and assign a random location to it.
function aiCreateWeapon(){
var b = Math.floor(Math.random()*ai.onHandWeapon.length);
$('#console').append(' ' + b + ' ');
var ip = 100;
while($('#'+ip).attr('class') != 'enemyField'){
ip = Math.floor(Math.random()*48);
}
encurrentWeapon = ai.onHandWeapon[b];
var source = encurrentWeapon.src;
var oImg = document.createElement("img");
oImg.setAttribute('src', source);
oImg.setAttribute('height', '60px');
oImg.setAttribute('width', '60px');
$('#'+ip).append(oImg).show('explode','slow');
encurrentWeapon.location = ip;
ai.onFieldWeapon.push( encurrentWeapon);
$('#console').append(' ' + ai.onFieldWeapon[0].location + ' ');
}
aiCreateWeapon is a function bind to a button. When I click it, the ai.onFieldWeapon[0].location is a fixed location until it changes. I have check that every time when the object which is same as the first element, is added to the onFieldWeapon Array, it will override the first element's data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该使用 splice
http://www.w3schools.com/jsref/jsref_splice.asp
you should use splice
http://www.w3schools.com/jsref/jsref_splice.asp
这就是向数组添加元素的方式。这样什么都不会被覆盖:
This is how you add an element to an array. This way nothing will be overwritten: