I have an object as follows:
JavaScript
x
47
47
1
{
2
"stage": [
3
{
4
"name": "Stage 1",
5
"rounds": [
6
{
7
"matches": [
8
{
9
"id": 1,
10
"start_at": "2021-04-01"
11
},
12
]
13
},
14
{
15
"matches": [
16
{
17
"id": 3,
18
"start_at": "2021-04-03"
19
}
20
]
21
}
22
]
23
},
24
{
25
"name": "Stage 2",
26
"rounds": [
27
{
28
"matches": [
29
{
30
"id": 7,
31
"start_at": "2021-04-07"
32
}
33
]
34
},
35
{
36
"matches": [
37
{
38
"id": 8,
39
"start_at": "2021-04-08"
40
}
41
]
42
}
43
]
44
}
45
]
46
}
47
I need to put all the values with the sauce key into a separate array so that I can create a menu. i need all “start_at” values inside a separate array, like:
JavaScript
1
6
1
[
2
"2021-04-01",
3
"2021-04-03",
4
"2021-04-04",
5
]
6
in vue.js i have access “start_at” values separately, but I want them all together
Advertisement
Answer
You can use flatMap to achieve this.
JavaScript
1
52
52
1
const obj = {
2
stage: [
3
{
4
name: "Stage 1",
5
rounds: [
6
{
7
matches: [
8
{
9
id: 1,
10
start_at: "2021-04-01",
11
},
12
],
13
},
14
{
15
matches: [
16
{
17
id: 3,
18
start_at: "2021-04-03",
19
},
20
],
21
},
22
],
23
},
24
{
25
name: "Stage 2",
26
rounds: [
27
{
28
matches: [
29
{
30
id: 7,
31
start_at: "2021-04-07",
32
},
33
],
34
},
35
{
36
matches: [
37
{
38
id: 8,
39
start_at: "2021-04-08",
40
},
41
],
42
},
43
],
44
},
45
],
46
};
47
48
const result = obj.stage.flatMap(({ rounds }) => {
49
return rounds.flatMap(({ matches }) => matches.flatMap((m) => m.start_at));
50
});
51
52
console.log(result);