There is some coding in this discussion. Feel free to write them in a REPL or in the comments below.
- How is an object different from an array?
- How does
const
work with objects? - Explain the difference between bracket syntax and dot syntax. Give an example of something that works with bracket syntax but not dot syntax. Give an example of something that works with dot syntax but not bracket syntax.
- What are computed properties? Write an example code.
- What is the difference between
Object.keys
andObject.entries
? Write example code using both of them. - How do we get only the values of an object?
// M.NOUR Krimesh, Abdul Malek, Ammar Almuain.
The difference between object and array is that object store its data as key:value and arrays store the data as indexed values.
const prevent us from defining another variable with the same name but we can change object's values.
Bracket notation allows you to access properties with special characters in their names, while you can not do this with dot notation
Computed properties allow you to dynamically choose what property in your object gets updated.
Object.keys(object) is a utility function that returns the list of keys of object for example:
const hero = {
name: 'Batman',
city: 'Gotham'
};
Object.keys(hero); // => ['name', 'city']
Object.entries(object) is an useful function to access the entries of object for example:
const hero = {
name: 'Batman',
city: 'Gotham'
};
Object.entries(hero); // =>
[['name', 'Batman'], ['city', 'Gotham']]
We get only the values of an object by using Object.values(object) for example:
const books = {
'book1': 5.50,
'book2': 10.00,
'book3': 4.35
};
const prices = Object.values(books);
prices; // => [4.35, 5.5, 10]