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?
Objects represent “things” with characteristics (aka properties), while arrays create and store lists of data in a single variable.
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.
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.
An example of something that works with bracket syntax but not dot syntax is accessing an object’s property by its index in an array. For example, if you have an array of objects, you can access the first object’s property using bracket syntax like this: myArray[0].propertyName. This would not work with dot syntax.
An example of something that works with dot syntax but not bracket syntax is accessing an object’s method by its name. For example, if you have an object with a method called “doSomething”, you can call it using dot syntax like this: myObject.doSomething(). This would not work with bracket syntax.
computed property is one that runs some code in order to calculate the value.
struct Person {
var name = "Taylor"
var favoriteColor = "red"
var favoriteCity = "Tokyo"
var favoriteFood = "tea"
var greeting: String {
return "Hello, my name is (name), and I like (favoriteFood), (favoriteCity), and the color (favoriteColor)."
}
}
Object. keys() method returns only the own property names and it only works for ES5 while Object. entries() method returns an array of arrays with key and value and it works from ES6
var object = {
2: 'Geeks1',
23: 'Geeks2',
52: 'Geeks3'
};
let valuesArray = Object.keys(object);
for (let value of valuesArray) {
document.write(value + "
");
}
Object.value(objectName);