0

I want to get values of a map (which has values as array) and store inside a string array in typescript.

myMap = {0:['a','b','c'], 1:['d','e'], 2:['f','g']};

Expected Result arr['a','b','c','d','e','f','g']

Updated: I haven't used flat() function before. Without flat() function it gives below result.

myMap = { 0: ["a", "b", "c"], 1: ["d", "e"], 2: ["f", "g"] };

const result = Object.values(myMap);
console.log(result);

1

2 Answers 2

1

Get all values of object using Object.values and then flat the array.

myMap = { 0: ["a", "b", "c"], 1: ["d", "e"], 2: ["f", "g"] };

const result = Object.values(myMap).flat();
console.log(result);

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

Comments

1

get the key and valueArray. the iterate through the valueArray.

const myMap = {0:['a','b','c'], 1:['d','e'], 2:['f','g']};
var arr = [];


for (const [key, valueArr] of Object.entries(myMap)) {
  for (var i = 0; i < valueArr.length; i++) {

    arr.push(valueArr[i]);
  }
}

console.log(arr);

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.