【问题标题】:Simpler way to combine a if else statement (.contains?)结合 if else 语句(.contains?)的更简单方法
【发布时间】:2011-07-11 19:19:23
【问题描述】:

简化此代码的最佳方法是什么。我考虑过使用 .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>';
          }
      }
        );

【问题讨论】:

  • 它们都是一样的,除了第二个说 value.brand 而不是 value.name
  • if (a||b){X} else if (a||c){X} 等价于if(a||b||c){X}

标签: javascript jquery arrays list


【解决方案1】:

看起来像是在顶部 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>';
      }

  }
    );

【讨论】:

  • 工作...谢谢。我以为我确实试过了,但一定是出了什么问题。感谢您的帮助。
【解决方案2】:

结合条件检查,因为您为两个代码路径推送相同的项目。

$.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>';
      }
  });

【讨论】:

    【解决方案3】:

    除非我遗漏了什么,否则两个分支都执行相同的代码。为什么不在第一个 if 中检查两个条件(即检查 value.name 或 value.brand 是否使用 || 包含过滤器值)?

    【讨论】:

    • 确实如此。我试过||在我的代码中的各个地方,但似乎我无法正确处理。它看起来很简单,但我肯定在这里遗漏了一些东西。
    【解决方案4】:

    首先,您应该同时检查两个条件(两个条件执行相同的代码)。其次,如果需要,可以使用条件运算符:

    variablename = (condition) ? value1 : value2;
    

    如果需要,您甚至可以内联使用它:

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

    【讨论】:

      【解决方案5】:

      你可以试试这样的:

      $.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>';
      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-12-03
        • 2016-11-04
        • 1970-01-01
        • 2011-05-26
        • 2013-08-18
        • 2012-11-26
        • 2012-03-31
        • 1970-01-01
        相关资源
        最近更新 更多