sports: 'ARod Hits Home Run',
business: 'GE Stock Dips Again',
movies: 'Superman Is A Flop'
}
This is the same data but the newspaper is presented in the form of images:
Objects are suitable for data sorted by title . When reading a newspaper, you usually don't read from the first to the last page. You can easily jump between sections or move to certain parts of your interest. No matter where the item is located, the reader quickly overcomes this section and reads the necessary information. This is the difference between a newspaper and a book, story lines and order of chapters that are very important.
Objects store information in each pair of properties / values (key / value pairs).
key: value
If you want to see the information in the Business section of the newspaper, you will use the "business" attribute to see the value:
newspaper['business']
or
newspaper.business
The above statement returns the value information contained in this ' GE Stock Díps Again ' attribute . So, the easiest way to get data is based on the title (or attribute), you should save the data as an object.
Combine arrays and objects
In the previous section, we talked about saving information in arrays or objects. In addition, you can also store other basic data types such as numbers and booleans objects in the following ways:
This can become a bit complicated. However, it is almost certain that you will need this combination to save data to help extend data later. You will definitely want to store it in a way that you can understand your code at a later week.
Let's review the example of the previous book. For example, if we need to save the page number of each chapter. We can save in the following ways:
var book =[
['foreword', 14],
['boywholived', 18]
]
var book = [
{name:'foreword', pageCount: 14},
{name:'boyWhoLived', pageCount: 18},
{name:'vanishingGlass', pageCount: 13},
{name:'lettersFromNoOne', pageCount: 17},
{name:'afterword', pageCount: 19}
];
We have maintained the order of chapters and now we have the ability to name specific features of each chapter. Therefore, if we want to know the page number of the second chapter, we can use:
book[1]['pageCount']
The result returns 18.
Now let's say you want to see the top writers' rankings for each section of the newspaper, based on your work time. You can express that in an array used in the newspaper object like:
var newspaper= {
sports: 'ARod Hits Home Run',
sportsWriters: ['Miramon Nuevo', 'Rick Reilly', 'Woddy Paige'], //nhà báo mục thể thao
business: 'GE Stock Dips Again',
businessWriters: ['Adam Smith', 'Albert Humphrey', 'Charles Handy'],
movies: 'Superman Is A Flop',
moviesWriters: ['Rogert Ebert', 'Andrew Sarris', 'Wesley Morris']
}
Arrays are a good choice to save journalists' names because we need to arrange them in order (work experience). Looking at it, you can understand that the previous journalist has more long-term work experience than the back press in each segment. Journalists arranged from index 0 are the oldest journalists.
In addition, you can also optimize the above newspaper by creating an object inside the newspaper object. For example, the " sports " property now has the value of an object, inside the " sports " object there are two " title " attributes that store the " sports " heading and the " writers " attribute that stores the array of houses. newspaper of " sports " section.
Here are a few small challenges for you:
Author: Kevin Kononenko
Refer to some more articles:
Having fun!