首页 > 解决方案 > 循环遍历数组并添加 IF 语句

问题描述

我试图让我的头脑围绕数组,并与它们一起使用 IF 语句。

我有我的数组:

var queryAlias = $('.TableQuery > tbody > tr').map(function () {
var $td2 = $(this).children('td');
  return `${$td2.eq(2).html()}([${$td2.eq(0).html()}].[${$td2.eq(1).html()}]) AS ${$td2.eq(4).html()}`; 
}).get().join(', '); /* COUNT([TABLE].[FIELD]) AS ALIAS */

该数组正在从我表中的某些列中获取数据,但我现在想要的是遍历数组并根据以下条件构建一个字符串,如果第 3 列为空,则将第 1 列和第 2 列添加为字符串:

$.each(queryAlias, function (index, value) {
  if ((`${queryAlias}`) == null) {
    queryAlias[index] = queryAlias[index];
  }
});

var field = $("#qBuilerFields option:selected").text();
var table = $("#qBuilerMaintable option:selected").text();
var orderby = $("#qBuilerSortBy option:selected").val();
var expression = $("#qBuilerExpression option:selected").val();
var alias = $("#alias").val();

/* Add new row to table with customers choices */
if ($("#qBuilerExpression").val() == null) {
  var addToTable = "<tr style='table-layout: fixed; width: 25%'><td>" + table + "</td><td>" + field + "</td><td style='table-layout: fixed; width: 25%'>" + expression + "</td><td style='table-layout: fixed; width: 25%'>" + orderby + "</td><td style='table-layout: fixed; width: 25%'>" + "[" + field + "]" + "</td><td><p style='color: darkred; text - align: center' class='pointer RemoveUser'>&#10006</p></td></tr>";
  $("table tbody").append(addToTable);
} else {
  var addToTable = "<tr style='table-layout: fixed; width: 25%'><td>" + table + "</td><td>" + field + "</td><td style='table-layout: fixed; width: 25%'>" + expression + "</td><td style='table-layout: fixed; width: 25%'>" + orderby + "</td><td style='table-layout: fixed; width: 25%'>" + alias + "</td><td><p style='color: darkred; text - align: center' class='pointer RemoveUser'>&#10006</p></td></tr>";
  $("table tbody").append(addToTable);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Drop down showing tables, this will be table A for any query -->
<select class="select" id="qBuilerMaintable">
  <option value="" disabled selected>{ Select Table... }</option>
  <option value="">TableA</option>
  <option value="">TableB</option>
</select>

<!-- Drop down showing fields linked to table A -->
<select class="select" id="qBuilerFields">
  <option value="" disabled selected>{ Select Field... }</option>
  <option value="">Field1</option>
  <option value="">Field2</option>
</select>

<!-- Alias, this will be the name of the column header if they enter text here  -->
<input class="input" id="alias" type="text" placeholder="(optional) Enter Alias...  " name="alias" value="" />

<!-- Drop down giving ORDER BY choices -->
<select class="select" id="qBuilerSortBy">
  <option value="" disabled selected>{ Select Order By... }</option>
  <option value="ASC">Ascending</option>
  <option value="DESC">Descending</option>
</select>

<!-- Expression -->
<select class="select" id="qBuilerExpression">
  <option value="" disabled selected>{ Select An Expression... }</option>
  <option value=""></option>
  <option value="count">Count</option>
  <option value="sum">Sum</option>
</select>

<table id="t01" class="TableQuery" style="table-layout:fixed">
  <thead>
    <tr style="table-layout:fixed">
      <th style="width:20%">Table</th>
      <th style="width:20%">Field</th>
      <th style="width:20%">Expression</th>
      <th style="width:10%">Order By</th>
      <th style="width:20%">Alias</th>
      <th style="width:10%">Remove</th>
    </tr>
  </thead>
  <tbody>
    <tr style="table-layout:fixed">

    </tr>
  </tbody>
</table>

标签: jquery

解决方案


推荐阅读