Map


    Resumo/Informações
    • Data Structure
      Maps possui a mesma ideia de objetos, porém com muito mais recursos.
      Possível de se utilizar:
      - forEach (porque está no prototype de maps).
      - Anything as keys, como strings, booleans, etc.
      - They are iterable, making it easy to loop through them and to manipulate data with them.
      - Easy to get the size of a map using the size property.
      - Easily add and remove data from a map.

    • Example
      const question = new Map();
      question.set('question', 'What is the official name of the latest major JavaScript version?');
      question.set(1, 'ES5'); Esses 1, 2, 3, 4 são Choice elements
      question.set(2, 'ES6');
      question.set(3, 'ES2015');
      question.set(4, 'ES7');
      question.set('correct', 3);
      question.set(true, 'Correct answer :D');
      question.set(false, 'Wrong, please try again!');

      console.log(question.get('question')); Irá dar print no elemento que possui question no primeiro Choice element.
      //console.log(question.size);

      if(question.has(4)) {
       //question.delete(4); Used to delete a question
       console.log('Answer 4 is here'); Se existir a question 4, isso ocorre
      }

      // question.clear(); clears everything, deletes it all
      // forEach example, onde todos os elementos são utilizados.
      question.forEach((value, key) => console.log(`This is ${key}, and it's set to ${value}`));
      O forEach passa em cada elemento do maps, recupera as informações value e key deles e então as utiliza na proxima ordem, nesse caso o console.log

      for (let [key, value] of
      question.entries()) {
       if (typeof(key) === 'number') {
        console.log(`Answer ${key};
        ${value}`);
       }
      }

      const ans = parseInt(prompt('Write the correct answer'));
      console.log(question.get(ans === question.get('correct'))); Compara a answer com a true answer, então se o input for o mesmo que a correct answer, isso será true