是否可以定位/跳转到特定的迭代/循环并输出它?

时间:2020-12-21 07:37:47

标签: javascript arrays

按照我下面的javascript代码:

function ready() {
  var items = document.querySelectorAll('.item')
  var data = [];
  for (var i = 0; i < items.length; i++) { 
    var current = items[i];
    var name = current.getElementsByClassName('item-title')[0].innerText;
    var price = parseInt(current.getElementsByClassName('item-price')[0].innerText);
    var products = {};
    products.title = name;
    products.cost = price;
    data.push(products);
    console.log(data)
 }

我得到以下结果:

[{title: "Jack Daniels 1L", cost: 60}] (1)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}] (2)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}] (3)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}, {title: "Grey Goose 1L", cost: 31}] (4)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}, {title: "Grey Goose 1L", cost: 31}, {title: "Remy Martins 750ml", cost: 45}] (5)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}, {title: "Grey Goose 1L", cost: 31}, {title: "Remy Martins 750ml", cost: 45}, {title: "Hennessy 1L", cost: 68}] (6)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}, {title: "Grey Goose 1L", cost: 31}, {title: "Remy Martins 750ml", cost: 45}, {title: "Hennessy 1L", cost: 68}, {title: "Johnnie Walker 1L", cost: 50}] (7)

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}, {title: "Grey Goose 1L", cost: 31}, {title: "Remy Martins 750ml", cost: 45}, {title: "Hennessy 1L", cost: 68}, {title: "Johnnie Walker 1L", cost: 50}, {title: "Double Black 750ml", cost: 55}] (8)

在每次迭代中,都会向后续数组添加一个对象,这意味着只有最后一个数组具有所需的所有内容。是否可以针对在第八个循环中产生的特定数组并将其作为结果单独记录?即这个特定的数组:

[{title: "Jack Daniels 1L", cost: 60}, {title: "Southern Comfort 2L", cost: 130}, {title: "Golden Label 1L", cost: 120}, {title: "Grey Goose 1L", cost: 31}, {title: "Remy Martins 750ml", cost: 45}, {title: "Hennessy 1L", cost: 68}, {title: "Johnnie Walker 1L", cost: 50}, {title: "Double Black 750ml", cost: 55}] (8)

3 个答案:

答案 0 :(得分:1)

如果您将 console.log() 移动到 for 循环的正下方,您会看到只有一个包含所有推送值的数组。根本不需要改变数组行为。

答案 1 :(得分:1)

试试这个:

function ready() {
  var items = document.querySelectorAll('.item');
  var data = [];

  for (var i = 0; i < items.length; i++) { 
    var current = items[i];
    var name = current.getElementsByClassName('item-title')[0].innerText;
    var price = parseInt(current.getElementsByClassName('item-price')[0].innerText);
    var products = {};
    products.title = name;
    products.cost = price;
    data.push(products);

    //if you want to see what data looks like on the 8th iteration specifically, console.log with an if condition:
    if(i === 7) {
      console.log(data);
    };
  };

  //if you want to see what data looks like after the loop, console.log here:
  console.log(data);

  //...
};

答案 2 :(得分:0)

您可以使用 if 语句来选择应添加的迭代:

for (var i = 0; i < items.length; i++) {
    if (i === 7)
    {
        var current = items[i];
        var name = current.getElementsByClassName('item-title')[0].innerText;
        var price = parseInt(current.getElementsByClassName('item-price')[0].innerText);
        var products = {};
        products.title = name;
        products.cost = price;
        data.push(products);
        console.log(data)
    }
 }

As mdn says:

<块引用>

if 语句在指定条件成立时执行语句 说实话。如果条件为假,则可以执行另一条语句。

相关问题