gpt4 book ai didi

JavaScript : array does not retain value

转载 作者:行者123 更新时间:2023-12-01 01:18:10 25 4
gpt4 key购买 nike

在下面的代码片段中,

function retreive_data_from_UI() {
let arr_rows = [];
cy.get(constants.cssCustomerWoListViewTable).children().each(($rows, ind) => {
arr_rows.push($rows.text());
cy.log(ind);
cy.log(arr_rows[ind]);
});
cy.wait(1000);
for(var i = 0; i < 5; i++){
// I tried both separately pop() and accessing by index
cy.log(arr_rows.pop());
// or
cy.log(arr_rows[i]);
}
return arr_rows;
}

arr_rows[ind] 的值在 cy.get().children().each(() => {}) block 中打印,但不在其后面的 for 循环中打印。下面是输出

Output

谁能指出我哪里错了?我正在使用 cypress 编写前端测试。

最佳答案

可能是这样,因为您正在声明 let arr_rows,这意味着 block 作用域。您正尝试将其填充到一个匿名函数中,该函数有自己的作用域,因此也有自己的arr_rows

使用 var arr_rows = [] 声明 arr_rows ,它应该可以工作。

参见here了解更多详情。

关于JavaScript : array does not retain value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54523491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com