I have the following structure:
JavaScript
x
6
1
ley objects = {
2
key1: [1, 2, 3],
3
key2: [3,4,6],
4
key3: [5, 6, 7],
5
}
6
How can I combine those arrays keeping any duplicates so I will have [1, 2, 3, 3, 4, 6, 6, 6, 7]
? I have tried concat
but I cannot seem to find a way to do so. I have many more keys so it has to be some loop:
My attempt so far:
JavaScript
1
5
1
let arr = []
2
for(const [key, value] of Object.entries(objects)){
3
arr.concat(value);
4
}
5
Would there be a possible way to avoid this loop?
Advertisement
Answer
You could flat the values from the array.
JavaScript
1
4
1
let object = { key1: [1, 2, 3], key2: [3, 4, 6], key3: [5, 6, 7] },
2
result = Object.values(object).flat();
3
4
console.log(result);