Utility to map one JavaScript object to another. Also turning it into JSON means to turn it into a string.Native javascript doesn't have any inbuilt serializer. But before getting there, let me show you one possible way of using the The array of array that you can see being passed to the So you can rebuild you map in a single line using the following sample code:Thanks for contributing an answer to Stack Overflow! Map.entries returns a new array for each value...so if you have 1000 values....you end up with 1000 arrays..and then you JSON every single one of them...this way is soo slow it makes it useless to use Maps in the first place.

By clicking “Post Your Answer”, you agree to our To subscribe to this RSS feed, copy and paste this URL into your RSS reader. The Map object holds key-value pairs and remembers the original insertion order of the keys. The Overflow Blog

It works but it makes it useless to use MapsThey have much more utility than just being able to use objects as keys They have the benefit of arrays being ordered, and the benefit of keys in objects, so you can get an item from the Map by a key, whether that is an object or a string, and you can loop a Map and be sure the items are in the right order.

It will look like [key, value].To avoid writing any looping code yourself, you can use Now, that's not even the coolest part.

The callback function accepts the following arguments:.

no it's not a string. site design / logo © 2020 Stack Exchange Inc; user contributions licensed under Certain functions are also commonly used with one argument, even though they take additional optional arguments.

If you'd like to contribute to the data, please check out Any value (both objects and primitive values) may be used as either a key or a value..

Certain functions within my Javascript rely on the specific type of object I am using. That’s the only way you could serialize maps: turning it into a string. Syntax let new_array = arr.map(function callback( currentValue[, index[, array]]) { // return element for new_array }[, thisArg]) Parameters callback. If you'd like to contribute to the interactive examples project, please clone The compatibility table in this page is generated from structured data. I am asking for a way to turn it into a string without using the JSON format..any other format will suffice as long as it doesnt create N new arrays and stringify every single one of them. your coworkers to find and share information.

For instance, lets use a phone number for example. When using AJAX, I tend to pass objects from my server to Javascript in the form of JSON objects (aka Javascript).

Function that is called for every element of arr.Each time callback executes, the returned value is added to new_array.. This is an example of a Google Maps JSON file which you might see used to store configuration settings to setup your system and google maps marker info.

Sign in to enjoy the benefits of an MDN account.

I’m not exactly sure what you mean by “you can’t pass Maps around as you could with objects”. Description. If you haven’t already created an account, you will be prompted to do so after signing in. You need use some good library for javascript.Well....the thing with this approach is that it relies on JSON standard...and it is slow. Where developers & technologists share private knowledge with coworkersProgramming & related technical career opportunitiesyou said that you can turn Map into JSON, why not Map -> JSON -> String?because turning it into JSON in the first place is a slow process and it makes it more costly over all to use Map than an Object even though a Map is faster to iterate over and do calculations. Stack Overflow for Teams is a private, secure spot for you and – user4642212 May 3 '18 at 10:59 The source for this interactive example is stored in a GitHub repository. Working with a single element - Working with a known value in an Object, you can access it directly with the key without the need to use a method, such as Map’s get() . currentValue The current element being processed in the array.

Stack Overflow works best with JavaScript enabled

I know that I could turn Map into JSON but it is costly to do so and it kind of looses the point of using Maps in the first place.JSON format is not meant for Maps...it was meant for Objects.Preferably this method should be as easy to perform as I guess the whole point of Maps/Dictionaries is that you can use So all that would only work for String keys, but that really takes most of power of maps, or in other words, reduces them to simple objects, what is the reason maps were implemented.the Map object has a method called entries() that returns an iterator that will yield each entry in the map as an array with 2 values in it. Key equality is based on the sameValueZero algorithm.

You could use JSON.stringify([...map.entries()]) for serializing and new Map(JSON.parse(string)) for parsing. There are two main functions: map and transform.They take a map from one reference list into an object to another reference list, and optionally one or more maps from the value at that location to a new value, and return a function that performs those maps when called on an object.

JSON - Objects work flawlessly with JSON.parse() and JSON.stringify(), two essential functions for working with JSON, a common data format that many REST APIs deal with.