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?
0- An object is a collection of properties, while an array is an ordered list of values. Objects are used to store keyed collections of various data and more complex entities, while arrays are used to store lists of values and can be accessed by index. Objects use key-value pairs to store data, while arrays use numbered indexes.
1- Const works with objects by making the object immutable, meaning that it cannot be changed or reassigned. This helps to ensure that the object remains consistent and secure. It also prevents accidental changes to the object.
2- The dot notation is used mostly as it is easier to read and comprehend and also less verbose. The main difference between dot notation and bracket notation is that the bracket notation allows us to access object properties using variable.
3-computed property is one that runs some code in order to calculate the value.
Ex:
struct Person {
var name = "Taylor"
var favoriteColor = "red"
var favoriteCity = "Tokyo"
var favoriteFood = "tea"
}
4- Object.keys is a method that returns an array of a given object's own enumerable property names, whereas Object.entries is a method that returns an array of a given object's own enumerable property [key, value] pairs.
Example code using Object.keys:
let obj = {name: 'John', age: 25};
let keys = Object.keys(obj);
console.log(keys); // Output: ['name', 'age']
Example code using Object.entries:
let obj = {name: 'John', age: 25};
let entries = Object.entries(obj);
console.log(entries); // Output: [['name', 'John'], ['age', 25]]
5- To get only the values of an object, you can use the Object.values() method. This will return an array containing all the values of the object's own enumerable properties