前端解決跨域的三種方法「jquery的each遍歷方法是幹什麼的」

jQuery的map方法以及和each方法的區別

整片代碼

<!DOCTYPE html>

<html>

<head>

<meta charset=”utf-8″>

<title>10-靜態方法-map方法</title>

<script src=”../static/js/jquery-3.6.0.js”></script>

<script>

// 定義一個數組

arr = [1, 3, 5, 7, 9]

// 定義個偽數組,就是一個對象

let obj = {0:1, 1:3, 2:5, 3:7, 4:9, length:5};

/*

// 利用原生JS的map方法遍曆數組

console.log(“利用原生JS的map方法遍曆數組”);

//value第一個參數: 當前遍歷到的元素

//index第二個參數: 當前遍歷到的索引

//array第三個參數:當前遍歷到的數組

// 和原生的foreach方法一樣只能遍曆數組,不能遍歷偽數組

arr.map(function(value, index, array){

console.log(index, value, array);

});jQuery的map方法以及和each方法的區別

利用原生JS的map方法遍曆數組

console.log(“利用原生JS的map方法遍歷偽數組{對象}”);

obj.map(function(value, index, array){

console.log(index, value, array);

});jQuery的map方法以及和each方法的區別

“利用原生JS的map方法遍歷偽數組{對象}

console.log(“下面是利用jQuery遍曆數組”)

// 利用jQuery遍曆數組 $.each(要遍歷的數組, 回調函數(索引, 值){執行的語 句})

// arr第一個參數: 要遍歷的數組

// function(value, index)第二個參數:每遍歷一個元素之後要執行的回調函數

// 回調函數的參數:

// value第一個參數: 遍歷到的元素

// index第二個參數: 遍歷到的索引

$.map(arr, function(value, index){

console.log(index, value);

})jQuery的map方法以及和each方法的區別

利用jQuery遍曆數組

console.log(“下面是利用jQuery遍歷偽數組(對象)”)

// 利用jQuery遍曆數組 $.each(要遍歷的數組, 回調函數(索引, 值){執行的語句})

$.map(obj, function(value, index){

console.log(index, value);

})jQuery的map方法以及和each方法的區別

利用jQuery遍歷偽數組(對象)

// 下面是jQuery的map和each之間的區別

console.log(“下面是jQuery的**map**和each之間的區別”)

let map = $.map(obj, function(value, index){

console.log(index, value);

})

console.log(map)jQuery的map方法以及和each方法的區別

下面是jQuery的**map**遍歷結果”

console.log(“下面是jQuery的map和**each**之間的區別”)

let each = $.each(obj, function(index, value){

console.log(index, value);

})

console.log(each)jQuery的map方法以及和each方法的區別

下面是jQuery的**each**遍歷結果

</script>

</head>

<body>

</body>

</html>

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/269045.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-16 13:13
下一篇 2024-12-16 13:13

相關推薦

發表回復

登錄後才能評論