I know there have been some similar questions before but I’m really stuck on trying to map the below array of information (I’ve tried to implement several example). I have an Array with two information fields and a third field that contains Arrays of objects. I want to extract the name of each of the objects into the original name so that my output looks like the below:
Desired Output:
JavaScript
x
2
1
[gameId, gameName, gameGenresArray]
2
Below is a sample of what the data looks like:
JavaScript
1
5
1
Data = [ 270722, 'The Wild at Heart', [ [Object], [Object], [Object] ] ],
2
[ 558984, 'Knockout City', [ [Object] ] ],
3
[ 558982, 'Miitopia', [ [Object], [Object] ] ],
4
[ 45775, 'Biomutant', [ [Object], [Object] ] ]
5
The [Object] has a property called gameGenre that I want to store in the original array as an array instead of as an Array of Objects.
My most recent attempt was:
JavaScript
1
2
1
var result = data.map(({ id, name, [{gameGenres}] }) => ([id, name, gameGenres]))
2
I appreciate any help anyone can add!
Thanks!!
Advertisement
Answer
I think this is what you want:
JavaScript
1
35
35
1
const Data = [
2
[
3
270722,
4
'The Wild at Heart', [{
5
name: 'action'
6
}, {
7
name: 'horror'
8
}, {
9
name: 'adventure'
10
}],
11
],
12
[558984, 'Knockout City', [{
13
name: 'action'
14
}]],
15
[558982, 'Miitopia', [{
16
name: 'action'
17
}, {
18
name: 'rpg'
19
}]],
20
[45775, 'Biomutant', [{
21
name: 'casual'
22
}, {
23
name: 'platform'
24
}]],
25
];
26
27
const result = Data.map(item => {
28
return {
29
gameId: item[0],
30
gameName: item[1],
31
gameGenresArray: item[2].map(genre => genre.name),
32
};
33
});
34
35
console.log(result);