Let's look at an object usage example. We'll write a function that takes the path to a file as input and returns information about the file as an object.
const filepath = '/path/to/index.js';
const fileinfo = getFileInfo(filepath);
// {
// extension: js
// filename: index.js
// };
Node.js has a built-in path module that can quickly retrieve the data you need. That's what you should use when writing real code, but here we're focusing not on how to get the data, but on how to create an object.
First, we need to extract the file name. This can be done using the split()
method.
import _ from 'lodash';
// Splitting the path into intermediate directories and file
const parts = filepath.split('/');
// Extracting the file name
// _.last fetches the last element in the array
const filename = _.last(parts);
You can do the same thing to get the file extension:
const extension = _.last(filename.split('.'));
Now, combining everything together, we implement the desired function:
const getFileInfo = (filepath) => {
const parts = filepath.split('/');
const filename = _.last(parts);
const extension = _.last(filename.split('.'));
// The desired values replace the variable values
const info = { filename: filename, extension: extension };
return info;
}
In the preceding example, the object is created as soon as all of the data is ready. When done differently, the object is initialized at the start and gradually filled with data:
const getFileInfo = (filepath) => {
// Object initialization
const info = {};
const parts = filepath.split('/');
const filename = _.last(parts);
info.filename = filename;
const extension = _.last(filename.split('.'));
info.extension = extension;
return info;
}
Which way do you prefer? In the vast majority of situations, the first way is better. When an object is created with all the data at once, its structure is obvious at a glance. In the second example, you have to run your eyes over the whole code to see what you end up with. On the other hand, the second method may be necessary for situations where the object is filled according to conditions that may not be met:
// add the property to the object only if the extension exists
if (extension) {
info.extension = extension;
}
It's much rarer, but it still happens.
Simplified object creation syntax
When an object is created immediately with all the data filled in, it often looks like the examples above:
const info = { filename: filename, extension: extension };
Note that the key name and the constant name that contains the value for this key are the same. This is such a common way to create objects that they add a simplified object creation syntax to JavaScript. If the name of the constant matches the property name in the object, you can simply add the constant name to the object definition without specifying the property:
const info = { filename, extension };
As real life has shown, this approach has proved to be very convenient and practical. In addition, it can also be combined with the usual way to create an object. JavaScript allows you to mix different ways of defining within a single object:
const filename = 'hexlet';
const ext = 'jpg';
const info = { filename, extension: ext };
// The order doesn't matter, we could have done it this way
const info = { extension: ext, filename };
// This is what the result will be
// const info = { filename: 'hexlet', extension: 'jpg' };
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
For full access to the course you need a professional subscription.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.