两个数组的赛普拉斯总和

问题描述 投票:1回答:1

enter image description here

大家好,我想对一个柏树中的两列值求和。

i声明了两个数组变量:数量和价格,并同时推送所有列元素。如果我记录数量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])
javascript arrays cypress
1个回答
1
投票

您可以在同一then中设置两个数组值:

cy.get('tr').then(($trs) => {
    // const quantity = [];
    // const price = []; 
    // if output be 'sum' all values can be stored in one array
    const values = [];
    $trs.each((idx, $el) => {
        const value1 = Number(cy.$$($el).find(':nth-child(10)> div > span:visible').text());
        values.push(Number(value1.toFixed(4)));

        const value2 = Number(cy.$$($el).find(':nth-child(14)> div > span:visible').text());
        values.push(Number(value2.toFixed(4)));
    });

    // sum it here
    cy.log(values);
    cy.log(values[1]);
    cy.log('Second Item:' + values[1]);
    cy.log('Fourth Item: ' + values[3]);
    cy.log('Sum: ' + values.reduce((sum, currentValue) => sum + currentValue));
});

我的测试截图:

enter image description here

对应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>
© www.soinside.com 2019 - 2024. All rights reserved.