1

I have a map returned by performing certain operations on a json data retrieved from a server. The map structure looks like follows when i print the key and values

action = [object Map]  
comedy = [object Map]  
thriller = [object Map]

And with in a object Map (i.e when i just print the values) they have counts which i expect.

Map {  
 'spanish' => 3  
 'english' => 4  
}  
Map {  
 'spanish' => 1  
 'english' => 2 
}  

I want to convert the nested map inside an array based something like this. is there already existing libraries i can leverage off?

var comedy[] = ['spanish_1', 'english_2']
var action[] =['spanish_3', 'english_4']

Please suggest if this is the ideal way to handle this data as i am finding it difficult to get the data out of the nested map.

2 Answers 2

1

Get the items in your map using Map.entries and iterate over it using Array.from to create a new Array having your desired values.

const myNewArray = Array.from(myMap.entries(), ([
  key,
  value,
]) => `${key}_${value}`);

EDIT

To get the following result :

{
   actions: [
     'spanish_3',
     ...
   ],

   comedy: [
    'spanish_3',
     ...
   ],
}

I loop on the first map level to feed a new json object that I fill with second level map data.

    Array.from(myMap.entries()).reduce((tmp, [
      xKey,
      xValue,
    ]) => ({
      ...tmp,

      [xKey]: Array.from(xValue.entries(), ([
        yKey,
        yValue,
      ]) => `${yKey}_${yValue}`),
    }), {});

To get the following output

[
   [
     'spanish_3',
     ...
   ],

   [
    'spanish_3',
     ...
   ],

   ...
]

Here I create a 2 dimensional array, that I use along with destructuration (the destructuration will work if you already know the data that's comming).

    const [
      action,
      comedy,
      thriller,
    ] = Array.from(myMap.entries(), ([
      xKey,
      xValue,
    ]) => Array.from(xValue.entries(), ([
      yKey,
      yValue,
    ]) => `${yKey}_${yValue}`));

There is plenty of use case, depends what info you have and what you want to do with it. Hope all examples will give you enough help to figure your specific use case out. :)

Sign up to request clarification or add additional context in comments.

9 Comments

You're missing parenthesis around [key, value].
So you have Maps in Maps rights? Like Map(actions -> Map(english -> 1) )
@thilak I've edited my answer, please tell me if it work I didn't tested it
@GrégoryNEUT It works Thank you , After the operation it generates a data structure which mentions it as an object. But i will have to figure out how to put the converted values specific to a key in a new array. Will play around. If you have any other suggestions on how to handle these kind of data it would be helpful as i am fairly new to it and i am not sure if i am doing the right thing
@thilak An other edit with an example of 2 dimensional array. Good luck with the next step!
|
0

You can convert it to an array and map the data to your desired format:

const map = new Map()
map.set('spanish', 3)
map.set('english', 4)

const result = Array.from(map).map(([key, value]) => `${key}_${value}`)

console.log(result) // ["spanish_3", "english_4"]

1 Comment

Hey Timo, The above in case doesn't print the values as expected above as each key has a object map corresponding to it, I need to print content from with in the object map. My current output is something as follows. Exa: ['action_object Map', 'comedy_object Map']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.