0

I need to return back from array to new Map data.

I have a array:


let test = [
['test' , true],
['test1' , true],
['test2' , true]
];


I need to set this to new Map like a :


{ 'test' => true }
{ 'test1' => true }
{ 'test2' => true }

 

What i am try ?


newMapData = [...new Map(test)];


But this is no work correctly.

I am using : https://www.w3schools.com/js/js_object_maps.asp

3
  • 3
    Just newMapData = new Map(test)? Commented Nov 15, 2022 at 20:37
  • Use the official documentation instead. In particular, see what parameters the Map constructor accepts. Commented Nov 15, 2022 at 20:40
  • Not sure why you're trying to spread the map. newMapData = new Map(test) works perfectly... Commented Nov 15, 2022 at 20:41

1 Answer 1

1

You can convert to the new ES6 Map simply do it without ...

let test = [
  ['test', true],
  ['test1', true],
  ['test2', true]
];
const newMapData = new Map(test);
// Unfortunately Maps don't show well with console.log:
for (let [key, value] of newMapData) {
  console.log(key + " = " + value);
}
// Map {"test" => true, "test1" => true, "test2" => true}

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.