簡體   English   中英

JavaScript中的if else / for循環

[英]If else/ for loops in JavaScript

有沒有什么辦法可以使這個if-else語句得到概括。 現在,TruthArray包含從t1到t4的值。 我正在尋找一種為任意數量的值t5,t6等創建通用解決方案的方法。 我沒有得到如何告訴for循環根據真值數組的長度創建越來越多的嵌套if語句的方法。 謝謝。 代碼說明:首先,我要檢查t1是否為true。 如果是這樣,那么positionArray中的第一個元素將為“ left”。 現在,當t1已經確定為true時,我想檢查t2的值。 如果t2為true,則將positionArray中的第二個元素設置為“ right”。 之后,當t1和t2已經為真時,我想檢查t3是否為真,並將positionArray的第三個元素設置為左。 然后我在t1,t2,t3都為真時檢查t4是否為真,依此類推。 因此,positionArray的連續元素將是右,然后是左,然后是右,然后是左,依此類推。 現在,如果t1不正確,那么我想從t2開始相同的過程。 如果t1不為真,則位置數組的第一個元素將不確定。 如果t1不是true且t2是true,則positionArray的第二個元素設置為'left',並且連續元素將是right,left,right等。如果t1和t2都不是true,則過程將從t3開始,依此類推。

  var truthArray = [t1, t2, t3, t4]; var positionArray = []; var i; if (truthArray[0] === true) { positionArray[0] = 'left'; if (truthArray[1] === true) { positionArray[1] = 'right'; if (truthArray[2] === true) { positionArray[2] = 'left'; if (truthArray[3] === true) { positionArray[3] = 'right'; } } else if (truthArray[3] === true) { positionArray[3] = 'left'; } } else if (truthArray[2] === true) { positionArray[2] = 'right'; if (truthArray[3] === true) { positionArray[3] = 'left'; } } else if (truthArray[3] === true) { positionArray[3] = 'right'; } } else if (truthArray[1] === true) { positionArray[1] = 'left'; if (truthArray[2] === true) { positionArray[2] = 'right'; if (truthArray[3] === true) { positionArray[3] = 'left'; } } else if (truthArray[3] === true) { positionArray[3] = 'right'; } } else if (truthArray[2] === true) { positionArray[2] = 'left'; if (truthArray[3] === true) { positionArray[3] = 'right'; } } else if (truthArray[3] === true) { positionArray[3] = 'left'; } 

希望這對您有所幫助,如果您遇到任何問題,請告訴我

var truthArray = [false, false, true]; 
var positionArray = [];
var positionArrayValues = ['left','right'];
var offsetArray = [];
var start = 0, end = truthArray.length;      
function traverseFurther(index,positionArrayIndex) {
    if(index === end)
        return;
    if(truthArray[index] === true) {
        positionArray[index] = positionArrayValues[positionArrayIndex];
        traverseFurther(index + 1, positionArrayIndex === 0 ? 1 : 0);
    } else {
        traverseFurther(index + 1,positionArrayIndex);
    }
}
traverseFurther(start, 0);

所以,你想通過循環truthValues ,並建立一個positionArray其中,如果 (truthValue ==假)添加undefinedpositionArray 再次添加或者'left''right'positionArray使得'left''right'總是交替?

這是您可以執行的操作。

  //fill truthArray with 10 random boolean values const truthArray = []; for (var i = 0; i < 10; i++) { truthArray.push(Math.random() > 0.5); } //calc(truthArray) and map the result (and truthInput) to make it easier to read const input = truthArray.map(t => t?'T':'F'); const output = calc(truthArray).map(v => { if (v === 'left') return 'L'; if (v === 'right') return 'R'; if (v === undefined) return '-'; }); //display truthInput and result console.log('T is true, F is false'); console.log('L is left, R is right, - is undefined'); console.log(input.join()); console.log(output.join()); function calc(truthArray) { var trueCount = 0; const result = []; for (var truthVal of truthArray) { if (truthVal) { const position = (trueCount % 2 == 0) ? 'left' : 'right'; result.push(position); trueCount++; } else { result.push(undefined); } } return result; } 

您可以映射支票並返回一個切換值或undefined

它通過檢查元素的值並使用帶有對象和值pos 的觸發器來工作。 通過將pos用作對象的屬性訪問器 ,可以通過為pos分配新值來更改此值。

 rl = { right: 'left', left: 'right' } pos = 'right'; pos = rl[pos]; // left pos = rl[pos]; // right pos = rl[pos]; // left pos = rl[pos]; // right // and so on ... 

如果元素不是true ,則將undefined映射到結果集。

 var truthArray = [false, true, false, true], lr = { right: 'left', left: 'right' }, pos = 'right', positionArray = truthArray.map(b => b ? pos = lr[pos] : undefined); console.log(positionArray); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

為了獲得不同的模式,例如

['left', 'left', 'right', 'right']

您可以增加索引並使用余數運算符對其進行調整,然后向右取一個位偏移的值,該值將值除以2並返回一個整數值。

 var truthArray = [true, false, true, false, true, false, true, false, true], lr = ['left', 'right'], pos = 3, positionArray = truthArray.map(b => b ? lr[++pos, (pos %= 4) >> 1] : undefined); console.log(positionArray); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

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

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