JavaScript Array copyWithin()
ตัวอย่าง
คัดลอกองค์ประกอบอาร์เรย์สองรายการแรกไปยังองค์ประกอบอาร์เรย์สองรายการสุดท้าย:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.copyWithin(2, 0);
คัดลอกสององค์ประกอบอาร์เรย์แรกไปยังตำแหน่งที่สามและสี่:
const fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.copyWithin(2, 0, 2);
ความหมายและการใช้งาน
วิธี การcopyWithin()
คัดลอกองค์ประกอบอาร์เรย์ไปยังตำแหน่งอื่นในอาร์เรย์
วิธี การcopyWithin()
เขียนทับค่าที่มีอยู่
เมธอด นี้copyWithin()
ไม่ได้เพิ่มรายการลงในอาร์เรย์
ไวยากรณ์
array.copyWithin(target, start, end)
พารามิเตอร์
Parameter | Description |
target | Required. The index (position) to copy the elements to. |
start | Optional. The start index (position). Default is 0. |
end | Optional. The end index (position). Default is the array length. |
คืนมูลค่า
พิมพ์ | คำอธิบาย |
อาร์เรย์ | อาร์เรย์ที่เปลี่ยนไป |
รองรับเบราว์เซอร์
copyWithin()
เป็นคุณลักษณะ ECMAScript6 (ES6)
ES6 (JavaScript 2015) ได้รับการสนับสนุนในเบราว์เซอร์ที่ทันสมัยทั้งหมด:
Chrome | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes |
copyWithin()
ไม่รองรับใน Internet Explorer 11 (หรือเก่ากว่า)