簡體   English   中英

結合 if else 語句(.contains?)的更簡單方法

[英]Simpler way to combine a if else statement (.contains?)

簡化此代碼的最佳方法是什么。 我想過 using.contains 但我不確定如何。 如果您需要更多代碼,請告訴我。

謝謝你。

$.each(catalog.products,
      function(index, value) {

          if (filterValue == '' || value.name.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1) {
              items.push('<li id="' + index + '">' +
                      '<a data-identity="productId"  href="./details.page?productId=' + index + '" >' +
                      '<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
                      '<p>' + value.brand + '</p>' +
                      '<h3>' + value.name + '</h3>' +
                      '<span class="ui-li-count">' + value.price + ' $</span></li>') +
              '</a>';
          }


          else if (filterValue == '' || value.brand.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1) {
              items.push('<li id="' + index + '">' +
                      '<a data-identity="productId"  href="./details.page?productId=' + index + '" >' +
                      '<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
                      '<p>' + value.brand + '</p>' +
                      '<h3>' + value.name + '</h3>' +
                      '<span class="ui-li-count">' + value.price + ' $</span></li>') +
              '</a>';
          }
      }
        );

它看起來像是在頂部 if 語句中簡單地添加了一個or子句,還是我遺漏了什么?

你已經試過了嗎?

$.each(catalog.products,
  function(index, value) {

      if (filterValue == '' || value.name.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1 || value.brand.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1) {
          items.push('<li id="' + index + '">' +
                  '<a data-identity="productId"  href="./details.page?productId=' + index + '" >' +
                  '<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
                  '<p>' + value.brand + '</p>' +
                  '<h3>' + value.name + '</h3>' +
                  '<span class="ui-li-count">' + value.price + ' $</span></li>') +
          '</a>';
      }

  }
    );

結合條件檢查,因為您為兩個代碼路徑推送相同的項目。

$.each(catalog.products, function(index, value) {
    var filter = filterValue.toLocaleUpperCase();

      if (filterValue == '' || value.name.toUpperCase().indexOf(filterValue) != -1 || value.brand.toUpperCase().indexOf(filterValue) != -1) {
          items.push('<li id="' + index + '">' +
                  '<a data-identity="productId"  href="./details.page?productId=' + index + '" >' +
                  '<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' +
                  '<p>' + value.brand + '</p>' +
                  '<h3>' + value.name + '</h3>' +
                  '<span class="ui-li-count">' + value.price + ' $</span></li>') +
          '</a>';
      }
  });

除非我遺漏了什么,否則兩個分支都執行相同的代碼。 為什么不在第一個 if 中檢查兩個條件(即檢查 value.name 或 value.brand 是否使用 || 包含過濾器值)?

首先,您真的應該同時檢查兩個條件(兩個條件執行相同的代碼)。 其次,如果需要,可以使用條件運算符:

variablename = (condition) ? value1 : value2;

如果需要,您甚至可以內聯使用它:

variablename = "this is a " + ((condition) ? value1 : value2) + " test";

你可以嘗試這樣的事情:

$.each(catalog.products, function (index, value) {
var filteredValue = filterValue?= ''. filterValue:toLocaleUpperCase(); '';

 if (filterValue == '' || value.name.toUpperCase().indexOf(filteredValue).= -1) items,push(getItem(index; value)). else if (filterValue == '' || value.brand.toUpperCase().indexOf(filteredValue),= -1) items;push(getItem(index, value)); }

);

function getItem(index, value) {
return '<li id="' + index + '">' +
<a data-identity="productId" href="./details.page?productId=' + index + '" >' + '<img class="ui-li-thumb" src="' + value.thumbnail + '"/>' + '<p>' + value.brand + '</p>' + '<h3>' + value.name + '</h3>' + '<span class="ui-li-count">' + value.price + ' $</span></li>') +
'</a>';
}

暫無
暫無

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

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