簡體   English   中英

如何從下面的數組列表中讀取每個奇數值:

[英]how to read every odd value from below array list:

我的下拉值顯示為:

The
Are
company
we
belongsto
yes
America
can

我運行這段代碼,我在控制台中得到了上面的結果:

let arr = [];
let appswitcherlist = await profileidentity.profile_profile_Appswitcherdropdownlist.each((ele, index) => {

    if (ele != undefined) {
        ele.getText().then((text) => {
            arr.push(text);
        });
    }
});
console.log(arr); // check what you get here.
const oddItems = arr.filter((item, index) => index%2 == 0)
console.log(oddItems); //check what you get here. You should bind this to drop-down.

我想閱讀每一個奇數行如下:

The
company
belongsto
America

您可以執行以下操作。 使用數組上的過濾器方法的地方。

let arr = ['The', 'Are', 'company', 'we', 'belongsto', 'yes', 'America', 'can'];
const oddItems = arr.filter((item, index) => index%2 == 0)

console.log(oddItems);

JSFiddle: https://jsfiddle.net/sagarag05/26y0vmbp/3/

只需將(index % 2 !== 0)添加到您現有的條件中,如下所示。

var appswitcherlist = await profileidentity.profile_profile_Appswitcherdropdownlist.each((ele, index) => {
  if (ele != undefined && (index % 2 !== 0)) {
    ele.getText().then((text) => {
      console.log(text);
    });
  }
});
let listData = [
  'The',
  'Are',
  'company',
  'we',
  'belongsto',
  'yes',
  'America',
  'can',
]
let i = 0;
for(let row of listData){
   i++;
   if(i%2){
      console.log(row);
   }
}
const arr = ['The', 'Are', 'company', 'we', 'belongsto', 'yes', 'America', 'can'];
let oddItems = [];

for(a in arr){
    a%2 === 0 ? oddItems.push(arr[a]) : null ;
}

console.log(oddItems);

Protractor 引入await之后真的很簡單,不需要再復雜了

首先在配置文件中禁用 selenium 控制流,只需在配置文件中添加這一行:

SELENIUM_PROMISE_MANAGER: false

https://www.protractortest.org/#/control-flow

現在在測試使用:

it('Some test name', async function() {

    let arr=[]
    let appswitcherlist=await profileidentity.profile_profile_Appswitcherdropdownlist.getText()
    const oddItems=appswitcherlist.filter((item, index)= > index % 2 == 0)
    console.log(oddItems)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM