การเก็บรวบรวมและทำซ้ำ วิธีการ ES6

ข้อกำหนด ECMAScript 6 แม้จะยังอยู่ในรูปแบบฉบับร่าง แต่ก็มีความน่าเชื่อถือว่าจะมีเครื่องมือใหม่ๆ ที่น่าตื่นเต้นมากมายเพิ่มเข้ามาในคลังของนักเขียนโปรแกรม JavaScript คลาสใหม่ เช่น Set และ Map นำเสนอโซลูชันแบบเนทีฟสำหรับการทำงานกับคอลเล็กชันบางประเภท และคำสั่ง for...of เป็นอีกทางเลือกหนึ่งที่น่าสนใจสำหรับการวนซ้ำข้อมูลแบบดั้งเดิม

Set เป็นวิธีติดตามคอลเล็กชันรายการที่แต่ละรายการจะปรากฏได้สูงสุด 1 ครั้ง Map มีฟังก์ชันการทำงานมากกว่าที่เคยทำได้ด้วยการใช้ Object เพื่อเชื่อมโยงคีย์กับค่า เมื่อใช้ Map คีย์ไม่จำเป็นต้องเป็นสตริง และคุณไม่ต้องกังวลว่าจะเลือกชื่อคีย์ที่ทับซ้อนกับชื่อเมธอดของ Object โดยไม่ได้ตั้งใจ การดำเนินการค้นหาใน Map และ Set เนทีฟจะดำเนินการในเวลาที่แน่นอน ซึ่งจะเพิ่มประสิทธิภาพได้มากกว่าการติดตั้งใช้งานแบบจําลอง

ตัวอย่างต่อไปนี้แสดงการสร้าง Set และใช้ for...of เพื่อวนซ้ำองค์ประกอบ

<pre id="log"></pre>

<script>
    function log() {
    document.querySelector('#log').textContent += Array.prototype.join.call(arguments, '') + '\n';
    }

    log('Creating, using, and iterating over a Set:');
    var randomIntegers = new Set();
    // Generate a random integer in the range [1..10] five times,
    // and use a Set to keep track of the distinct integers that were generated.
    for (var i = 0; i < 5; i++) {
    randomIntegers.add(Math.floor(Math.random() * 10) + 1);
    }
    log(randomIntegers.size, ' distinct integers were generated.');
    log('The number 10 was ', randomIntegers.has(10) ? '' : 'not ', 'one of them.');
    log('Here\'s all of them:');
    // Use for...of to iterate over the items in the Set.
    // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-iteration-statements
    // The Set iterator yields a single value corresponding to each entry in the Set.
    for (var item of randomIntegers) {
    log(item);
    }
</script>

ต่อไปนี้คือตัวอย่างที่เกี่ยวข้องซึ่งแสดงการใช้และการทำซ้ำ Map

<script>
    log('\nCreating and iterating over a Map:');
    // Maps can be initialized by passing in an iterable value (https://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-iterable)
    // Here, we use an Array of Arrays to initialize. The first value in each sub-Array is the new
    // Map entry's key, and the second is the item's value.
    var typesOfKeys = new Map([
    ['one', 'My key is a string.'],
    ['1', 'My key is also a string'],
    [1, 'My key is a number'],
    [document.querySelector('#log'), 'My key is an object']
    ]);
    // You can also call set() to add new keys/values to an existing Map.
    typesOfKeys.set('!!!!', 'My key is excited!');

    // Use for...of to iterate over the items in the Map.
    // https://people.mozilla.org/~jorendorff/es6-draft.html#sec-iteration-statements
    // There are several types of Map iterators available.
    // typesOfKeys.keys() can be used to iterate over just the keys:
    log('Just the keys:');
    for (var key of typesOfKeys.keys()) {
    log('  key: ', key);
    }

    // typesOfKeys.values() can be used to iterate over just the values:
    log('Just the values:');
    for (var value of typesOfKeys.values()) {
    log('  value: ', value);
    }

    // The default Map iterator yields an Array with two items; the first is the Map entry's key and the
    // second is the Map entry's value. This default iterator is equivalent to typesOfKeys.entries().
    log('Keys and values:');
    // Alternative, ES6-idiomatic syntax currently supported in Safari & Firefox:
    // for (var [key, value] of typesOfKeys) { … }
    for (var item of typesOfKeys) {
    log('  ', item[0], ' -> ', item[1]);
    }
</script>

เบราว์เซอร์บางรายการ เช่น Chrome, Internet Explorer และ Firefox ได้เพิ่มการรองรับ Set และ Map แล้ว การรองรับแบบเนทีฟที่เสริมด้วยไลบรารี polyfill เช่น es6-collections หรือ es6-shim ช่วยให้นักพัฒนา JavaScript เริ่มสร้างด้วยคอลเล็กชันประเภทใหม่เหล่านี้ได้ตั้งแต่วันนี้ ยังไม่มี polyfill สำหรับคำสั่ง for...of (แต่สามารถแปลงโค้ดผ่าน Traceur ได้) แต่ปัจจุบันมีการสนับสนุนแบบเนทีฟใน Chrome และ Firefox

ข้อมูลอัปเดตเดือนกันยายน 2014: ลิงก์กับตัวเลือก polyfill เพิ่มเติมอย่าง es6-shim