最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對象,Map()方法詳解以及數(shù)組中查詢某值是否存在

 更新時間:2019年01月18日 10:21:12   作者:muzidigbig  
今天小編就為大家分享一篇關(guān)于Jquery遍歷篩選數(shù)組的幾種方法和遍歷解析json對象|Map()方法詳解以及數(shù)組中查詢某值是否存在,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

1.jquery grep()篩選遍歷數(shù)組(可以得到反轉(zhuǎn)的數(shù)組)

// 1.jquery grep()篩選遍歷數(shù)組(可以得到反轉(zhuǎn)的數(shù)組)
  var array = [1,5,9,3,12,4,48,98,4,75,2,10,11];
  var filterArray = $.grep(array,(currentValue) => {
    return currentValue > 10;
  });
  console.log(`${filterArray}---${filterArray.length}`);//12,48,98,75,11---5
  var filterReverse = $.grep(array,(currentValue) => {
    return currentValue > 10;
  },true);
  console.log(`${filterReverse}---${filterReverse.length}`);//1,5,9,3,4,4,2,10---8
 
  // for(var i=0;i<filterArray.length;i++){
  //   console.log(filterArray[i]);
  // }
  for(key in filterArray){
    console.log(filterArray[key])
  }

2.filter()篩選遍歷數(shù)組(與grep()不同的是調(diào)用者不同,參數(shù)不同)

var ages = [32, 33, 16, 40, 45, 98, 12, 35, 8,16];
  var filterAges = ages.filter((currentValue,index,ages) => {
    return currentValue > 20;
  })
  console.log(filterAges);//[32, 33, 40, 45, 98, 35]
  for(key in filterAges){
    console.log(filterAges[key])
  }

3.jquery each()篩選遍歷數(shù)組(主要用來遍歷對象)

var anObject = {one:1,two:2,three:3};//對json數(shù)組each
  $.each(anObject,function(name,value) {
    console.log(`${name}---${value}`)
  });
  var anArray = ['one','two','three'];
  $.each(anArray,function(n,value){
     console.log(`${n}---${value}`)
  });

4.jquery forEach()篩選遍歷數(shù)組

var forArray = ['mu','zi','muzi','digbig','muzidigbig'];
  forArray.forEach((currentValue,index,forArray) => {
    console.log(`${index}---${currentValue}`)
  })

5.jquery map()篩選遍歷數(shù)組

var strings = ['0','1','2','3','4','S','6'];
  var values = $.map(strings,function(value){
      var result = new Number(value);
      return isNaN(result) ? null:result;//isNaN:is Not a Number的縮寫
    });
  for (key in values) {
    console.log(values[key]);
  }

6.jquery inArray()篩選遍歷數(shù)組(用于查找某個值第一次在數(shù)組中出現(xiàn)的位置)

var iArray = ['one','two','three','two'];
  var index = $.inArray('two',anArray);
  console.log(`返回該值在數(shù)組中的鍵值:${index}`);//返回該值在數(shù)組中的鍵值,返回 1
  console.log(`返回該鍵在數(shù)組中的值:${iArray[index]}`);//two

7.indexOf()用于查找某個值第一次在數(shù)組中出現(xiàn)的位置(存在返回第一次出現(xiàn)的索引值,不存在返回-1)

var iArray = ['one','two','three','two'];
  var indexOf = iArray.indexOf('two');
  console.log(indexOf);//1

8.includes()(判斷數(shù)組中是否存在某個值返回Boolean類型)

var iArray = ['one','two','three','two'];
  var index = iArray.includes('two');
  console.log(index);//true

二、遍歷解析json對象

1.遍歷json 1

var json = [{dd:'SB',AA:'東東',re1:123},{cccc:'dd',lk:'1qw'}];
for(var i=0,l=json.length;i<l;i++){
  for(var key in json[i]){
  console.log(`${key}:${json[i][key]}`);
  }
}

2、jquery遍歷解析json對象 2

有如下 json對象:

var obj ={'name':'馮娟','password':'123456','department':'技術(shù)部','sex':'女','old':30};

遍歷方法:

var obj ={'name':'馮娟','password':'123456','department':'技術(shù)部','sex':'女','old':30};
var str = '';
for(var p in obj){
  str += obj[p]+',';
//  return str;
}
console.log(str);//馮娟,123456,技術(shù)部,女,30,

三、Map()方法詳解

1、實(shí)例

構(gòu)建表單中所有值的列表:

$("p").append( $("input").map(function(){
 return $(this).val();
}).get().join(", ") );

2、定義和用法

map() 把每個元素通過函數(shù)傳遞到當(dāng)前匹配集合中,生成包含返回值的新的 jQuery 對象。

3、語法

.map(callback(index,domElement))

參數(shù)

描述

callback(index,domElement)  對當(dāng)前集合中的每個元素調(diào)用的函數(shù)對象。

詳細(xì)說明

由于返回值是 jQuery 封裝的數(shù)組,使用 get() 來處理返回的對象以得到基礎(chǔ)的數(shù)組。

.map() 方法對于獲得或設(shè)置元素集的值特別有用。請思考下面這個帶有一系列復(fù)選框的表單:

<form method="post" action="">
 <fieldset>
  <div>
   <label for="two">2</label>
   <input type="checkbox" value="2" id="two" name="number[]">
  </div>
  <div>
   <label for="four">4</label>
   <input type="checkbox" value="4" id="four" name="number[]">
  </div>
  <div>
   <label for="six">6</label>
   <input type="checkbox" value="6" id="six" name="number[]">
  </div>
  <div>
   <label for="eight">8</label>
   <input type="checkbox" value="8" id="eight" name="number[]">
  </div>
 </fieldset>
</form>

我們能夠獲得復(fù)選框 ID 組成的逗號分隔的列表:

$(':checkbox').map(function() {
 return this.id;
}).get().join(',');

本次調(diào)用的結(jié)果是字符串:"two,four,six,eight"。

在 callback 函數(shù)內(nèi)部,this 引用每次迭代的當(dāng)前 DOM 元素。該函數(shù)可返回單獨(dú)的數(shù)據(jù)項(xiàng),或者是要被插入結(jié)果集中的數(shù)據(jù)項(xiàng)的數(shù)組。如果返回的是數(shù)組,數(shù)組內(nèi)的元素會被插入集合中。如果函數(shù)返回 null 或 undefined,則不會插入任何元素。

若有不足請多多指教!希望給您帶來幫助!

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論

天津市| 朝阳县| 义马市| 中方县| 荥阳市| 昌图县| 华亭县| 永春县| 东乌珠穆沁旗| 始兴县| 蓝田县| 扬州市| 义乌市| 九江县| 饶阳县| 色达县| 合山市| 明水县| 伽师县| 许昌市| 颍上县| 荔浦县| 莱芜市| 河北省| 辽源市| 临朐县| 大田县| 丰顺县| 蒲江县| 军事| 沈阳市| 北安市| 廉江市| 五家渠市| 宁波市| 济宁市| 重庆市| 喜德县| 呈贡县| 咸宁市| 通化县|