I have worked myself into a state of confusion by being curious.
The solution for the Movies Exercise for the Javascript course shows a chained manipulation on the array. If I understand correctly, each of the manipulations return a new array.
But if I break up the chained manipulations into each step and observe the output, the output is not coming out as I expected?
app.js
const movies = [
{title: 'a', year: 2018, rating: 4.5},
{title: 'b', year: 2018, rating: 4.7},
{title: 'c', year: 2018, rating: 3},
{title: 'd', year: 2018, rating: 4.2},
{title: 'e', year: 2018, rating: 4.1},
{title: 'g', year: 2017, rating: 3},
{title: 'h', year: 2018, rating: 4.9}
];
let titles1 = movies
.filter(m => m.year === 2018 && m.rating >= 4);
let titles2 = titles1
.sort((a,b) => a.rating - b.rating);
let titles3 = titles2
.reverse();
let titles4 = titles3
.map(m => m.title);
console.log(βmoviesβ,movies);
console.log(βtitles1β,titles1);
console.log(βtitles2β,titles2);
console.log(βtitles3β,titles3);
console.log(βtitles4β,titles4);
output
movies [
{ title: βaβ, year: 2018, rating: 4.5 },
{ title: βbβ, year: 2018, rating: 4.7 },
{ title: βcβ, year: 2018, rating: 3 },
{ title: βdβ, year: 2018, rating: 4.2 },
{ title: βeβ, year: 2018, rating: 4.1 },
{ title: βgβ, year: 2017, rating: 3 },
{ title: βhβ, year: 2018, rating: 4.9 }
]
titles1 [
{ title: βhβ, year: 2018, rating: 4.9 },
{ title: βbβ, year: 2018, rating: 4.7 },
{ title: βaβ, year: 2018, rating: 4.5 },
{ title: βdβ, year: 2018, rating: 4.2 },
{ title: βeβ, year: 2018, rating: 4.1 }
]
titles2 [
{ title: βhβ, year: 2018, rating: 4.9 },
{ title: βbβ, year: 2018, rating: 4.7 },
{ title: βaβ, year: 2018, rating: 4.5 },
{ title: βdβ, year: 2018, rating: 4.2 },
{ title: βeβ, year: 2018, rating: 4.1 }
]
titles3 [
{ title: βhβ, year: 2018, rating: 4.9 },
{ title: βbβ, year: 2018, rating: 4.7 },
{ title: βaβ, year: 2018, rating: 4.5 },
{ title: βdβ, year: 2018, rating: 4.2 },
{ title: βeβ, year: 2018, rating: 4.1 }
]
titles4 [ βhβ, βbβ, βaβ, βdβ, βeβ ]
My question is, shouldnβt each console log display the respective results of each step in the chained manipulation?
eg. console.log(titles1) is displaying a sorted array?
I may have misunderstood something fundamentally important. Many thanks and hope you are all safe and well in these whacky times.
B