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

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แถว reduce() Method

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

ตัวอย่าง

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

var numbers = [65, 44, 12, 4];

function getSum(total, num) {
    return total + num;
}
function myFunction(item) {
    document.getElementById("demo").innerHTML = numbers.reduce(getSum);
}

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

125
ลองตัวเอง»

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


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

class="color_h1">reduce () วิธีการลดอาร์เรย์เป็นค่าเดียว

reduce() วิธีการดำเนินการจัดให้มีฟังก์ชั่นสำหรับค่าของอาร์เรย์แต่ละ (from left-to-right)

ค่าตอบแทนของฟังก์ชั่นจะถูกเก็บไว้ในสะสม (result/total)

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


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

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

วิธี
reduce() ใช่ 9.0 3.0 4 10.5

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

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

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

กลับค่า: บูลีน ผลตอบแทนจริงถ้าใด ๆ ขององค์ประกอบในอาร์เรย์ผ่านการทดสอบมิฉะนั้นก็จะส่งกลับเท็จ
JavaScript เวอร์ชัน: 1.8

ตัวอย่าง

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

ตัวอย่าง

ตลอดทั้งจำนวนเป็นอาร์เรย์และแสดงผลรวม:

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

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

<script>
var numbers = [15.5, 2.3, 1.1, 4.7];

function getSum(total, num) {
    return total + Math.round(num);
}
function myFunction(item) {
    document.getElementById("demo").innerHTML = numbers.reduce(getSum,0);
}
</script>
ลองตัวเอง»

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