-1

I have 40 arrays each has a value. What I want is to convert that into 1 array containing the 40 values. The array comes in a GET. My POST only accepts an Array with list of values. How do I achieve this in JavaScript?

 handleSubmit(e){
        e.preventDefault();

        const post_url = "/send_report";

        let data = JSON.stringify( {
            recipient: this.state.email,
            rooms: [this.state.roomIdNo]
        });
roomIdNo:
Array[48]
0:
"room01"
1:
"room02"
2:
"room03"
3:
"room04"
4:
"room05"
5:
"room42"
6:
"room43"
........
...........

["room1", "room2", "room3", "room4"]```
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Jose
  • 99
  • 1
  • 8

2 Answers2

1

You can use reduce for that:

const array = [
    ['a', 'b'],
    ['c']
]

const result =
    array.
reduce((a, b) => [...a, ...b])

console.log(result)
Jose A. Ayllón
  • 866
  • 6
  • 19
1

Here you go, a simple combination of reduce and a spread operator:

const arrayOfArrays = [["a"],["b"],["c"]];
const flatArray = arrayOfArrays.reduce((pv, cv) => [...pv, ...cv], []);
console.log(flatArray);
fjc
  • 5,590
  • 17
  • 36