如何检查数组中的值是否大于使用jQuery

时间:2017-11-27 13:44:48

标签: javascript jquery arrays

我有部门。在每个部门都有几个名称面板。如果一个部门有超过3个面板名称,那么请给我一个部门通知。

我遍历部门并将面板长度推送到数组。 现在我应该创建类似$.inArray()的东西,如果数组中有一个匹配的值只返回索引。但不幸的是,我不知道如何检查多个值。



$(document).ready(function() {
  $countDepartmentPanels = [];
  $("div[id^=department-]").each(function(index, value) {
    $countDepartmentPanels.push(value.children.length);
  });

  console.log($countDepartmentPanels);
});

@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
  margin: 10px;
}

<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-sm-3">
      <div id="department-1">
        <div class="panel panel-default">
          <div class="panel-body">Name 1.1</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 1.2</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 1.3</div>
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div id="department-2">
        <div class="panel panel-default">
          <div class="panel-body">Name 2.1</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 2.2</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 2.3</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 2.4</div>
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div id="department-3">
        <div class="panel panel-default">
          <div class="panel-body">Name 3.1</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 3.2</div>
        </div>
      </div>
    </div>
    <div class="col-sm-3">
      <div id="department-4">
        <div class="panel panel-default">
          <div class="panel-body">Name 4.1</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 4.2</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 4.3</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 4.4</div>
        </div>
        <div class="panel panel-default">
          <div class="panel-body">Name 4.5</div>
        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

这是JSFIDDLE

1 个答案:

答案 0 :(得分:0)

试试这个。这是fiddle

$(document).ready(function() {

    $countDepartmentPanels = [],indexArray=[];

    $("div[id^=department-]").each(function(index, value) {

    $countDepartmentPanels.push(value.children.length);

  }); 
  $countDepartmentPanels.map(function(value,index){
         if(value > 2){
        indexArray.push({index: index,value:value})

       }
  })
  console.log($countDepartmentPanels);
  console.log(indexArray);
});