简体   繁体   English

对于循环循环太多次

[英]For loop looping one too many times

I have some code: 我有一些代码:

var cart = [];
var items = [];
var cart_node = document.querySelectorAll('#tblItineraryModuleStayDetail > tbody > tr');
var cart_as_array = Array.prototype.slice.call(cart_node, 2); // start at item 3 (2)
for(var i=0;i<cart_as_array.length;i+=2) {
    items.push(cart_as_array[i]);
}

Now, in the console if I type items I get: 现在,在控制台中,如果输入内容,我将得到:

在此处输入图片说明

So I expect the loop to go around once in this instance. 因此,我希望在这种情况下循环会绕过一次。

Here's my loop: 这是我的循环:

for(i=0; i < items.length; i++) {
    // set vars
    cart[i] = {};
    var name = items[i].querySelector('.txtStayRoomDescription').textContent;
    var price = items[i].querySelector('.tblItinPriceSummary tr td:last-child').textContent;
    var brand = items[i].querySelector('.txtStayRoomLocation').textContent;

    // add to object
    cart[i].name = name;
    cart[i].price = price;
    cart[i].brand = brand;

    // add to cart array
    cart.push(cart[i]);
  }

Which gives: 这使:

在此处输入图片说明

I expected a result with array cart containing one item object not two. 我期望阵列购物车包含一个而不是两个项目对象的结果。 But it has two identical objects. 但是它有两个相同的对象。

What's going on here? 这里发生了什么?

You're first setting the ith element of the cart array as your object, then also pushing it onto the end; 首先,将cart数组的第i个元素设置为对象,然后将其推到末尾。 this will put two copies in, as you see. 如您所见,这将放入两份副本。

Edit for question in comments: 编辑评论中的问题:

Let's go through your code line by line: 让我们逐行浏览代码:

for(i=0; i < items.length; i++) {
    // set vars
    cart[i] = {};

After cart[i] = {} puts an empty object in the cart array at index i ; cart[i] = {}将一个空对象放入cart数组中的索引i if there was something there before, it will be overwritten, otherwise the array will simply be added. 如果之前有东西,它将被覆盖,否则将简单地添加数组。

// stuff setting properties removed

// add to object
cart[i].name = name;
cart[i].price = price;
cart[i].brand = brand;

Now, the object at cart[i] has received the attributes you constructed. 现在,在cart[i]处的对象已收到您构造的属性。 The cart array now contains an object with these name, price, and brand attributes at position i . 现在, cart数组在位置i处包含具有这些名称,价格和品牌属性的对象。

// add to cart array
cart.push(cart[i]);

Now, in addition to the reference at i , you've pushed a second reference to the object stored at i on to the end of the array. 现在,除了在i处的引用外,您还将对存储在i处的对象的第二个引用推到了数组的末尾。 This will produce the behavior you are observing: the object will be in the array twice. 这将产生您正在观察的行为:该对象将在数组中两次。

I would recommend changing cart[i] = {} (and the associated code that adds properties of this object) to construct the object while it is stored in a local variable, then push it on to the array at the end of the loop. 我建议更改cart[i] = {} (以及添加此对象属性的相关代码),以在将对象存储在局部变量中的同时构造该对象,然后在循环结束时将其压入数组。

At line 3 of your code you create an empty object in your array: 在代码的第3行,您在数组中创建一个空对象:

cart[i] = {};

then at line 14 you push that object into your array again : 然后在第14行,将该对象再次推入数组:

cart.push(cart[i]);

You should instead just create an object, and push it at the end: 相反,您应该只创建一个对象,然后将其推入末尾:

var item = {};
// ... add properties to item ...
cart.push(item)

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

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