I have below array of objects. As you can see, i have 2 runTypes named VEGGIES
and FRUITS
. Each runType
will have a list of verticals to it. For e.g. VEGGIES
has SPINACH, TOMATO, ONION
and FRUITS has APPLE, BANANA, GRAPES
JavaScript
x
45
45
1
let data = [
2
{
3
"runType": "VEGGIES",
4
"verticals": [
5
{
6
"vertical": "SPINACH",
7
"radars": {}
8
},
9
{
10
"vertical": "TOMATO",
11
"radars": {}
12
},
13
{
14
"vertical": "ONION",
15
"radars": {}
16
},
17
],
18
"total_count": {}
19
},
20
{
21
"runType": "FRUITS",
22
"verticals": [
23
{
24
"vertical": "APPLE",
25
"radars": {
26
27
}
28
},
29
{
30
"vertical": "BANANA",
31
"radars": {}
32
},
33
{
34
"vertical": "GRAPES",
35
"radars": {
36
"P5": 8
37
}
38
}
39
],
40
"total_count": {
41
"P5": 8
42
}
43
}
44
]
45
In my case, i want to extract these vertical
values and put them in an array. In case of regular array of objects, we can achieve the above task by using this code.
JavaScript
1
2
1
let result = data.map(({ verticals }) => vertical)
2
But my code has array of objects inside an array of object. Can someone please let me know how to achieve these 3 scenarios
- Scenario 1- Get all the verticals for both runType. Result should be [SPINACH, TOMATO, ONION, APPLE, BANANA, GRAPES]
- Scenario 2- get all verticals for runType = ‘VEGGIES’. Result should be [SPINACH, TOMATO, ONION]
- Scenario 3- get all verticals for runType = ‘FRUITS’. Result should be [APPLE, BANANA, GRAPES]
Can someone pls shed some light on this particular data.
Advertisement
Answer
In addition to map
the OP also might have a look into flatMap
and find
… reduce
is also worth a try …
JavaScript
1
95
95
1
let data = [{
2
"runType": "VEGGIES",
3
"verticals": [{
4
"vertical": "SPINACH",
5
"radars": {},
6
}, {
7
"vertical": "TOMATO",
8
"radars": {},
9
}, {
10
"vertical": "ONION",
11
"radars": {},
12
}],
13
"total_count": {},
14
}, {
15
"runType": "FRUITS",
16
"verticals": [{
17
"vertical": "APPLE",
18
"radars": {},
19
}, {
20
"vertical": "BANANA",
21
"radars": {},
22
}, {
23
"vertical": "GRAPES",
24
"radars": {
25
"P5": 8,
26
},
27
}],
28
"total_count": {
29
"P5": 8,
30
},
31
}];
32
33
// Scenario 1
34
// - Get all the verticals for both runType.
35
// - Result should be [SPINACH, TOMATO, ONION, APPLE, BANANA, GRAPES]
36
console.log('Scenario 1 ... ', data
37
38
.flatMap(({ verticals }) =>
39
verticals.map(({ vertical }) => vertical)
40
)
41
);
42
43
// Scenario 2
44
// - get all verticals for runType = 'VEGGIES'.
45
// - Result should be [SPINACH, TOMATO, ONION]
46
console.log('Scenario 2 ... ', data
47
48
.find(item => item.runType === 'VEGGIES')
49
.verticals.map(({ vertical }) => vertical)
50
);
51
52
// Scenario 3
53
// - get all verticals for runType = 'FRUITS'.
54
// - Result should be [APPLE, BANANA, GRAPES]
55
console.log('Scenario 3 ... ', data
56
57
.find(item => item.runType === 'FRUITS')
58
.verticals.map(({ vertical }) => vertical)
59
);
60
61
62
// Bonus
63
// - based on Array#reduce one can achieve everything at once
64
65
function groupMergeAndCollectVerticals(collector, item) {
66
const { index, list } = collector;
67
const { runType, verticals } = item;
68
69
const group = (index[runType] ??= []);
70
const verticalList = verticals.map(({ vertical }) => vertical);
71
72
group.push(verticalList);
73
list.push(verticalList);
74
75
return collector;
76
}
77
const verticalsCollection =
78
data.reduce(groupMergeAndCollectVerticals, { index: {}, list: [] });
79
80
console.log(
81
'reduce based :: all at once ... ',
82
verticalsCollection
83
);
84
console.log(
85
'reduce based :: Scenario 1 ... ',
86
verticalsCollection.list
87
);
88
console.log(
89
'reduce based :: Scenario 2 ... ',
90
verticalsCollection.index['VEGGIES']
91
);
92
console.log(
93
'reduce based :: Scenario 3 ... ',
94
verticalsCollection.index['FRUITS']
95
);
JavaScript
1
1
1
.as-console-wrapper { min-height: 100%!important; top: 0; }