I have the following structure:
ley objects = {
key1: [1, 2, 3],
key2: [3,4,6],
key3: [5, 6, 7],
}
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:
let arr = []
for(const [key, value] of Object.entries(objects)){
arr.concat(value);
}
Would there be a possible way to avoid this loop?
Advertisement
Answer
You could flat the values from the array.
let object = { key1: [1, 2, 3], key2: [3, 4, 6], key3: [5, 6, 7] },
result = Object.values(object).flat();
console.log(result);