简体   繁体   English

在运行时在javascript数组中添加对象

[英]Adding object in javascript array at runtime

The mRegion object adds last object multiple times, however the objBeacon prints different objects. mRegion对象多次添加最后一个对象,但是objBeacon打印不同的对象。 What is wrong with the mRegion? mRegion有什么问题?

      var mRegion = new Array();

      var objBeacon = {
          id: '10',
name:'name',   
          description: 'description'
      };
      $.ajax(settings).done(function(response) {
                  // populate beacon registry in an array
                  for (var i in response.items[0].devices) {
                      objBeacon.id = response.items[0].devices[i].id;
                      objBeacon.name = response.items[0].devices[i].name;
                      objBeacon.description = response.items[0].devices[i].description;


                      console.log("value of i is" + i);
                      console.log(objBeacon);

                      mRegion.push(objBeacon);
                  }
                  console.log(mRegion);

Objects in javascript are passed by reference. javascript中的对象通过引用传递。 You only have one variable objBeacon and each array element is pointing to this variable. 您只有一个变量objBeacon并且每个数组元素都指向该变量。 Whenever you change objBeacon , all references will change. 每当您更改objBeacon ,所有引用都会更改。

var mRegion = [];

$.ajax(settings).done(function(response) {
  // populate beacon registry in an array
  for (var i in response.items[0].devices) {
    mRegion.push({
      id: response.items[0].devices[i].id,
      uid: '00',
      major: 1,
      minor: 1,
      name: response.items[0].devices[i].name,
      description: response.items[0].devices[i].description
    });
  }
});

You only ever create one object and assign a reference to it to objBeacon . 您只能创建一个对象,并将其引用分配给objBeacon

Each time you go around the loop you modify the single object you have and push an additional reference to it into the array. 每次您遍历循环时,都会修改您拥有的单个对象,并将对该对象的其他引用推入数组。

If you want an array of different objects, you need to create a new object each time you go around the loop. 如果要使用一组不同的对象,则每次循环时都需要创建一个新对象。

As you are using objects your using "references" instead of "clones". 使用对象时,将使用“引用”而不是“克隆”。

That code should work (even it is not very beautifull) 该代码应该工作(即使它不是很漂亮)

var mRegion = new Array();


$.ajax(settings).done(function(response) {
    // populate beacon registry in an array
    for (var i in response.items[0].devices) {
        var objBeacon = {
            id: '10',
            uid: '00',
            major: 1,
            minor: 1,
            name: 'name',
            description: 'description'
        };

        objBeacon.id = response.items[0].devices[i].id;
        objBeacon.name = response.items[0].devices[i].name;
        objBeacon.description = response.items[0].devices[i].description;


        console.log("value of i is" + i);
        console.log(objBeacon);

        mRegion.push(objBeacon);
    }
    console.log(mRegion);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM