如何在javascript中将相同的元素插入到数组中?

发布于 2024-12-17 06:02:31 字数 1596 浏览 0 评论 0原文

我有一系列对象。但是当我插入之前添加的对象时,它将覆盖我之前的对象。我该如何解决它?

我有一个叫做玩家的对象。在播放器中,我有两个数组:一个名为 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 技术交流群。

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

发布评论

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

评论(2

凉城 2024-12-24 06:02:31

你应该使用 splice

http://www.w3schools.com/jsref/jsref_splice.asp

<script type="text/javascript">

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write("Added: " + fruits.splice(2,0,"Lemon") + "<br />");
document.write(fruits);

</script>

Added:
Banana,Orange,Lemon,Apple,Mango

you should use splice

http://www.w3schools.com/jsref/jsref_splice.asp

<script type="text/javascript">

var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write("Added: " + fruits.splice(2,0,"Lemon") + "<br />");
document.write(fruits);

</script>

Added:
Banana,Orange,Lemon,Apple,Mango
农村范ル 2024-12-24 06:02:31

这就是向数组添加元素的方式。这样什么都不会被覆盖:

var sports = ["soccer", "baseball"];
sports.push("football", "swimming");

This is how you add an element to an array. This way nothing will be overwritten:

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