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?
Cengiz aksakal, Amjad maqsouma, gizem haspolat, sobhan shams, Rama aldakkak:
The arrays cannot have a pair of values and keys. It is true that can have different types but we they cannot be like objects.
Objects can have properties, we just write the key and its value will pop out. arrays only have values and index numbers.
const means constant by reference, which means that we cannot change place of the object in the memory but it can be modified in the terms of values.
obj.name (Dot notation)
the disadvantages:
it cannot be used with numbers with special characters like obj.0
the advantages:
it is easier to use because you do not have to inisialize variables or be aware of qoutations marks.
obj["name"] (Brackets notation)
the advantages:
we can use it with special characters
obj["1stname 2nd name"]
we can use it with computed properties
const customers = {
firstName: "John",
lastName: "Doe",
email: "[email protected]"
};
console.log(customers.firstName);
// Output: John
const value = "firstName";
console.log(customers[value]);
// Output: John
const customers = {
firstName: "John",
lastName: "Doe",
email: "[email protected]"
};
console.log(customers.firstName);
// Output: John
const value = "firstName";
console.log(customers[value]);
// Output: John
Example :
const object1 = {
a: 'string',
b: 50
};
for (const [key, value] of Object.keys(object1)) {
console.log(
${key}: ${value}
);}
// expected output:
// "a"
// "b"
The Object.entries() method in JavaScript returns an array consisting of enumerable property [key, value] pairs of the object.
Example :
const object1 = {
a: 'string',
b: 50
};
for (const [key, value] of Object.entries(object1)) {
console.log(
${key}: ${value}
);}
// expected output:
// "a: string"
// "b: 50"
const object1 = {
a: 'string',
b: 50
};
for (const [key, value] of Object.values(object1)) {
console.log(
${key}: ${value}
);}
// expected output:
// "string"
// "50"