ล่าสุดการพัฒนาเว็บบทเรียน
×

JavaScript การอ้างอิง

ภาพรวม

JavaScript

JS เชือก JS จำนวน JS ผู้ประกอบการ JS งบ JS คณิตศาสตร์ JS วันที่ JS แถว JS บูลีน JS นิพจน์ทั่วไป JS สถานการณ์โดยรวม JS การแปลง

เบราว์เซอร์ BOM

Window Navigator Screen History Location

HTML DOM

DOM เอกสาร DOM องค์ประกอบ DOM แอตทริบิวต์ DOM เหตุการณ์ DOM รูปแบบ

HTML วัตถุ

<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> search <input> submit <input> text <input> time <input> url <input> week <kbd> <keygen> <label> <legend> <li> <link> <map> <mark> <menu> <menuitem> <meta> <meter> <nav> <object> <ol> <optgroup> <option> <output> <p> <param> <pre> <progress> <q> <s> <samp> <script> <section> <select> <small> <source> <span> <strong> <style> <sub> <summary> <sup> <table> <td> <th> <tr> <textarea> <time> <title> <track> <u> <ul> <var> <video>

วัตถุอื่น ๆ

CSSStyleDeclaration


 

JavaScriptแถว forEach() Method

<อ้างอิงอาร์เรย์ JavaScript

ตัวอย่าง

รายชื่อรายการในอาร์เรย์แต่ละ

<button onclick="numbers.forEach(myFunction)">Try it</button>
<p id="demo"></p>

<script>
demoP = document.getElementById("demo");
var numbers = [4, 9, 16, 25];

function myFunction(item, index) {
    demoP.innerHTML = demoP.innerHTML + "index[" + index + "]: " + item + "<br />";
}
</script>

ผลที่จะได้รับ:

index[0]: 4
index[1]: 9
index[2]: 16
index[3]: 25
ลองตัวเอง»

เพิ่มเติม "Try it Yourself" ตัวอย่างด้านล่าง


ความหมายและการใช้งาน

forEach() วิธีการเรียกฟังก์ชั่นที่มีให้ครั้งเดียวสำหรับองค์ประกอบในอาร์เรย์แต่ละในการสั่งซื้อ

หมายเหตุ: forEach() ไม่ได้ดำเนินการฟังก์ชันสำหรับองค์ประกอบมากมายโดยไม่ต้องค่า


สนับสนุนเบราว์เซอร์

ตัวเลขในตารางระบุราว์เซอร์รุ่นแรกที่สนับสนุนอย่างเต็มที่วิธีการ

วิธี
forEach() ใช่ 9.0 1.5 ใช่ ใช่

วากยสัมพันธ์

ค่าพารามิเตอร์
พารามิเตอร์ ลักษณะ
function(currentValue, index,arr) จำเป็นต้องใช้ ฟังก์ชั่นที่จะทำงานสำหรับองค์ประกอบในอาร์เรย์แต่ละ
ข้อโต้แย้งฟังก์ชั่น:
การโต้เถียง ลักษณะ
currentValue จำเป็นต้องใช้ ค่าขององค์ประกอบปัจจุบัน
index ไม่จำเป็น. ดัชนีอาร์เรย์ขององค์ประกอบปัจจุบัน
arr ไม่จำเป็น. อาร์เรย์วัตถุองค์ประกอบปัจจุบันเป็น
thisValue ไม่จำเป็น. ค่าที่จะถูกส่งผ่านไปยังฟังก์ชั่นที่จะนำมาใช้เป็นของ "this" คุ้มค่า
ถ้าพารามิเตอร์นี้เป็นที่ว่างเปล่าค่า "undefined" จะถูกส่งผ่านเป็นของ "this" คุ้มค่า

รายละเอียดทางเทคนิค

กลับค่า: ไม่ได้กำหนด
JavaScript เวอร์ชัน: 1.6

ตัวอย่าง

ตัวอย่างอื่น ๆ

ตัวอย่าง

ได้รับผลรวมของค่าทั้งหมดในอาร์เรย์:

<button onclick="numbers.forEach(myFunction)">Try it</button>

<p>Sum of numbers in array: <span id="demo"></span></p>

<script>
var sum = 0;
var numbers = [65, 44, 12, 4];

function myFunction(item) {
    sum += item;
    demo.innerHTML=sum;
}
</script>
ลองตัวเอง»

ตัวอย่าง

คูณค่าทั้งหมดในอาร์เรย์กับจำนวนที่ระบุ:

<p>Multiply with: <input type="number" id="multiplyWith" value="10"></p>
<button onclick="numbers.forEach(myFunction)">Try it</button>

<p>Updated array: <span id="demo"></span></p>

<script>
var numbers = [65, 44, 12, 4];

function myFunction(item,index,arr) {
    arr[index] = item * document.getElementById("multiplyWith").value;
    demo.innerHTML=numbers;
}
</script>
ลองตัวเอง»

<อ้างอิงอาร์เรย์ JavaScript