loading
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const data = [
  {
    author : {
      firstname : 'isaac',
      lastname  : 'asimov'
    },
    books : [
      {
        tags : ['Foundation cycle', 'Addition to trilogy'],
        title : 'Prelude to Foundation',
        publishedYear : 1988
      },{
        tags : ['Foundation cycle', 'Addition to trilogy'],
        title : 'Forward the Foundation',
        publishedYear : 1993
      },{
        tags : ['Foundation cycle', 'Original trilogy'],
        title : 'Foundation',
        publishedYear : 1951
      },{
        tags : ['Foundation cycle', 'Original trilogy'],
        title : 'Foundation and Empire',
        publishedYear : 1952
      },{
        tags : ['Foundation cycle', 'Original trilogy'],
        title : 'Second Foundation',
        publishedYear : 1953
      },{
        tags : ['Foundation cycle', 'Addition to trilogy'],
        title : 'Foundation\'s Edge',
        publishedYear : 1982
      },{
        tags : ['Foundation cycle', 'Addition to trilogy'],
        title : 'Foundation and Earth',
        publishedYear : 1986
      }
    ]
  }
]

const asimovData = data.find( d => d.author.lastname === 'asimov')

console.log('------- filter -------')
const originalTrilogy = asimovData.books.filter( book => {

  return book.tags.indexOf('Original trilogy') > -1

})

console.log(originalTrilogy)

console.log('------- map -------')
const originalTrilogyTitleWithTags = asimovData.books.map(book => {

  return {
    title : book.title,
    tagsToDisplay : book.tags.join(', ')
  }

})

console.log(originalTrilogyTitleWithTags)


console.log('------- reduce -------')
const newOrganization = asimovData.books.reduce((newData, currentBook) => {

  currentBook.tags.filter(tag => tag !== 'Foundation cycle').forEach(tag => {

    if(!newData[tag]) newData[tag] = []

    newData[tag].push({
      title : currentBook.title,
      year  : currentBook.publishedYear
    })

  })

  return newData

}, {})

newOrganization
node v10.16.0