2

I have an array of Map<string, number>

How can I flat to a single map?

Map<string, number>[] to Map<string, number>

Thank you

3
  • With a loop? ... Commented Oct 30, 2020 at 9:06
  • 1
    Do you mind providing a sampled data here you're dealing with? Commented Oct 30, 2020 at 9:06
  • what happens with duplicate keys? Commented Oct 30, 2020 at 9:17

1 Answer 1

1

You can use Array.reduce and create a new Map with the result:

let map1 = new Map([['a', 1], ['b', 2]]);
let map2 = new Map([['c', 3], ['d', 4]]);
let maps = [map1, map2];

// reduce to a flattened array
let arr = maps.reduce((acc, val) => [...acc, ...val], []);

// create a new map using that array
let map3 = new Map(arr);
Sign up to request clarification or add additional context in comments.

Comments

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.