Javascript 组合if-else语句(.contains?)的更简单方法

Javascript 组合if-else语句(.contains?)的更简单方法,javascript,jquery,arrays,list,Javascript,Jquery,Arrays,List,简化此代码的最佳方法是什么。我考虑过使用.contains,但我不确定如何使用。如果需要更多代码,请告诉我 多谢各位 $.each(catalog.products, function(index, value) { if (filterValue == '' || value.name.toUpperCase().indexOf(filterValue.toLocaleUpperCase()) != -1) { items.push

简化此代码的最佳方法是什么。我考虑过使用.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(filterValue=''|| value.name.toUpperCase().indexOf(filterValue.tolocaluppercase())!=-1){
items.push(“
  • ”+ ''; } else if(filterValue=''|| value.brand.toUpperCase().indexOf(filterValue.tolocaluppercase())!=-1){ items.push(“
  • ”+ ''; } } );
  • 除非我遗漏了什么,否则两个分支执行相同的代码。为什么不在第一个if中同时检查两个条件(即使用| |检查value.name或value.brand是否包含筛选值)?

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

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

    variablename = (condition) ? value1 : value2;
    
    如果需要,您甚至可以内联使用它:

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

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

    variablename = (condition) ? value1 : value2;
    
    如果需要,您甚至可以内联使用它:

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

    它看起来像是在if语句顶部添加了一个
    子句,还是我遗漏了什么

    你已经试过这个了吗

    $.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>';
          }
    
      }
        );
    
    $。每个(目录.产品,
    函数(索引、值){
    如果(filterValue=''|| value.name.toUpperCase().indexOf(filterValue.toLocaleUpperCase())!=-1 || value.brand.toUpperCase().indexOf(filterValue.toLocaleUpperCase())!=-1){
    items.push(“
  • ”+ ''; } } );
  • 它看起来像是在if语句顶部添加了一个
    子句,还是我遗漏了什么

    你已经试过这个了吗

    $.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>';
          }
    
      }
        );
    
    $。每个(目录.产品,
    函数(索引、值){
    如果(filterValue=''|| value.name.toUpperCase().indexOf(filterValue.toLocaleUpperCase())!=-1 || value.brand.toUpperCase().indexOf(filterValue.toLocaleUpperCase())!=-1){
    items.push(“
  • ”+ ''; } } );
  • 组合条件检查,因为您正在为两个代码路径推送相同的项

    $.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>';
          }
      });
    
    $。每个(目录、产品、功能(索引、值){
    var filter=filterValue.toLocaleUpperCase();
    如果(filterValue=''|| value.name.toUpperCase().indexOf(filterValue)!=-1 || value.brand.toUpperCase().indexOf(filterValue)!=-1){
    items.push(“
  • ”+ ''; } });
  • 组合条件检查,因为您正在为两个代码路径推送相同的项

    $.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>';
          }
      });
    
    $。每个(目录、产品、功能(索引、值){
    var filter=filterValue.toLocaleUpperCase();
    如果(filterValue=''|| value.name.toUpperCase().indexOf(filterValue)!=-1 || value.brand.toUpperCase().indexOf(filterValue)!=-1){
    items.push(“
  • ”+ ''; } });
  • 您可以尝试以下方法:

    
    $.each(目录、产品、功能(索引、值){
    var filteredValue=filterValue!=''?filterValue.toLocaleUpperCase():''

    ));

    
    函数getItem(索引,值){
    返回“
  • ”+
    “;
    }


  • 您可以尝试以下方法:

    
    $.each(目录、产品、功能(索引、值){
    var filteredValue=filterValue!=''?filterValue.toLocaleUpperCase():''

    ));

    
    函数getItem(索引,值){
    返回“
  • ”+
    “;
    }


    (代码)名称:<代码>如果(a)代码>如果(a)代码>如果(a)代码>如果(a)a||||||||||||||||;;|||||||||||;;; c)b)b){b{X{X}如果(a{X}

    如果(a
  • 代码>相当于(代码>如果(代码>如果(a)如果(代码>如果(a)如果(a)如果(a)如果(a)如果(a)a 124124124124124124124124124124124124124124||124;b)b{X}他们有。我在代码中的不同位置尝试了| |,但似乎我做得不对。它看起来很简单,但我确实遗漏了一些东西。他们有。我在代码中的不同位置尝试了| |,但似乎我做得不对。它看起来很简单,但我确实遗漏了一些东西。工作……谢谢。我想我已经尝试过了很抱歉,但一定是出了什么问题。谢谢你的帮助。很好…谢谢。我想我确实试过了,但一定是出了什么问题。谢谢你的帮助。