From e845c39b4e985a4954e5a38ecd9387f5d3e433b9 Mon Sep 17 00:00:00 2001 From: Yohan Choi Date: Wed, 30 Oct 2019 22:47:06 -0400 Subject: [PATCH] adding more exercise to map_object.js --- JavaScript_Advance/map_object.js | 46 +++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/JavaScript_Advance/map_object.js b/JavaScript_Advance/map_object.js index 7219d42..3cc2e30 100644 --- a/JavaScript_Advance/map_object.js +++ b/JavaScript_Advance/map_object.js @@ -59,4 +59,48 @@ for (const amount of recipeMap.values()) { // iterate over [key, value] entries for (const entry of recipeMap) { // the same as of recipeMap.entries() alert(entry); // cucumber,500 (and so on) -} \ No newline at end of file +} + +/* +Loop with forEach +Instead of using for loop, forEach can be used. + +map.forEach(function(value, key){ + +}); + +or with array function + +map.forEach((value, key) => { + +}) + +*/ + +recipeMap.forEach((value, key) => { + alter(key + " = " + value); +}); + +/* +Convert To Arrays +A map obejct can be convert to arrays + +var array = Array.from(map) - create an array of the key-value pairs + +var array = Array.from(map.value()) - Create an array of the values + +var array = Array.from(map.key()) - Create an array of the keys + +*/ + +// create an array of the key-value pairs +const keyValArr = Array.from(recipeMap); +alter(keyValArr); + +// Create an array of the values +const ValArr = Array.from(recipeMap); +alter(ValArr); + +// Create an array of the keys +const keyArr = Array.from(recipeMap); +alter(keyArr); \ No newline at end of file