【问题标题】:Cypress Sum of two arrayCypress 两个数组的总和
【发布时间】:2020-05-29 22:18:34
【问题描述】:

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

我声明了两个数组变量:数量和价格,并推送所有列元素。 如果我记录数量 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】:

    您可以在同一个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>
    

    【讨论】:

    • Number($el.eq(9).find('> div > span:visible').text()) 这只会给我一个值。如何从行中获取所有值?
    • 我不知道为什么 Quantity 的行为不像数组。 cy.log(quantity[1]) 这不起作用
    • 请检查修改后的答案。
    • 请检查我的问题:
    • 请重新检查修改后的答案。
    【解决方案2】:

    非常感谢。我曾经使用下面的两个数组求和。它的工作。

    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].

    如何用两位小数固定

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-11
      • 2016-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多