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

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

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

ตัวอย่าง

กลับอาร์เรย์ที่มีรากที่สองของค่าทั้งหมดในอาร์เรย์เดิม:

var numbers = [4, 9, 16, 25];

function myFunction() {
    x = document.getElementById("demo")
    x.innerHTML = numbers.map(Math.sqrt);
}

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

2,3,4,5
ลองตัวเอง»

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


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

map() วิธีการสร้างอาร์เรย์ใหม่กับผลของการเรียกฟังก์ชั่นสำหรับองค์ประกอบอาร์เรย์ทุก

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

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

หมายเหตุ: map() ไม่ได้เปลี่ยนอาร์เรย์เดิม


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

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

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

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

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

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

กลับค่า: อาร์เรย์ที่มีผลของการเรียกฟังก์ชั่นที่จัดไว้ให้สำหรับแต่ละองค์ประกอบในอาร์เรย์เดิม
JavaScript เวอร์ชัน: 1.6

ตัวอย่าง

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

ตัวอย่าง

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

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

function multiplyArrayElement(num) {
    return num * document.getElementById("multiplyWith").value;
}

function myFunction() {
    document.getElementById("demo").innerHTML = numbers.map(multiplyArrayElement);
}
ลองตัวเอง»

ตัวอย่าง

ได้รับชื่อเต็มของแต่ละคนในอาร์เรย์:

var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];


function getFullName(item,index) {
    var fullname = [item.firstname,item.lastname].join(" ");
    return fullname;
}

function myFunction() {
    document.getElementById("demo").innerHTML = persons.map(getFullName);
}
ลองตัวเอง»

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