- Open your command-line application, and navigate to your workspace.
- Create a new folder named 10-05-reduce-to-transform-data.
- Create a main.js file that defines a new class named Rocket that takes a constructor argument name and assigns it to an instance property:
// main.js
class Rocket {
constructor(name) {
this.name = name;
}
}
- Create a main function with an array of nationality strings:
// main.js
export function main() {
const nationalities = [
'American',
'American',
'Chinese',
'American',
'Chinese',
'Chinese',
'Soviet',
'Soviet'
];
}
- Use the reduce method to count the different nationalities:
// main.js
export function main() {
//...
const nationalityCount = nationalities.reduce((acc, nationality) => {
acc[nationality] = acc[nationality] || 0;
acc[nationality] ++;
return acc;
}, {});
console.log('Nationalities:', nationalityCount);
}
- Start your Python web server and open the following link in your browser:
http://localhost:8000/.
- You should see the following output:
..................Content has been hidden....................
You can't read the all page of ebook, please click
here login for view all page.