大家好,我想对一棵柏树中的两列值求和。
我声明了两个数组变量:数量和价格,并同时推送所有列元素。
如果我记录数量cy.log(quantity),它将显示数组中的所有值。但是cy.log(quantity[1])
无法正常工作。价格数组相同。
我不知道为什么它的行为不像数组。
如果我声明,a = [1,2,3,4,5,6]然后cy.log(a[1])
,则可以正常工作
我必须对两个数组求和。尝试了很多方法。
var quantity =[]
var price =[]
cy.get('td:nth-child(10) > div > span:visible').each(($el, index, $list) =>
{
quantity.push(Number($el.text())).toFixed(4)
})
cy.get('td:nth-child(14) > div > span:visible').each(($el, index, $list) =>
{
price.push(Number($el.text())).toFixed(4)
})
cy.log(quantity) // working
cy.log(price) // working
cy.log(quantity[1]) //not working
cy.log(price[3]) //not working
// this part is working:
var a=[1,2,3,4,5,6]
cy.log(a[3])
答案 0 :(得分:1)
您可以在同一then
中设置两个数组值:
cy.get('tr').then(($trs) => {
const price = [];
const quantity = [];
$trs.each((idx, $el) => {
const value1 = Number(cy.$$($el).find(':nth-child(10)> div > span:visible').text());
price.push(Number(value1.toFixed(4)));
const value2 = Number(cy.$$($el).find(':nth-child(10)> div > span:visible').text());
quantity.push(Number(value2.toFixed(4)));
});
const sum = price.map((p, index) => (p + quantity[index]).toFixed(2));
cy.log('Price: ' + price);
cy.log('Quantity: ' + quantity);
cy.log('price[1]: ' + price[1]);
cy.log('quantity[1]: ' + quantity[1]);
cy.log('Sum = ' + sum);
});
我的测试截图:
对应的Markup
:
<table>
<tr>
<td>5.42342</td>
<td>2.442</td>
<td>6.767678</td>
<td>6767.678</td>
</tr>
<tr>
<td>7.6343543</td>
<td>8.44</td>
<td>84.554</td>
<td>24</td>
</tr>
</table>
答案 1 :(得分:0)
非常感谢。我以前使用以下两个数组求和。它的工作。
model=[]
lower=[]
var squares = model.map((a, i) => a.toFixed(4) - lower[i].toFixed(4));
但是我的结果是:
[0.01, 0.019999999999999997, 0.060000000000000005, 0.04, 0.09000000000000001, 0.019999999999999997, 0.08, 0.35, 0.12000000000000001, 0.07, 0.03]
。
如何用小数点后两位固定