Skip to content

Instantly share code, notes, and snippets.

@halitbatur
Forked from louisrli/discussion.md
Created April 29, 2021 16:54
Show Gist options
  • Save halitbatur/10774b50c5bcdce2ce5cf9d8d8c8e609 to your computer and use it in GitHub Desktop.
Save halitbatur/10774b50c5bcdce2ce5cf9d8d8c8e609 to your computer and use it in GitHub Desktop.
Object discussion questions

Object Discussion Questions

There is some coding in this discussion. Feel free to write them in a REPL or in the comments below.

  1. How is an object different from an array?
  2. How does const work with objects?
  3. 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.
  4. What are computed properties? Write an example code.
  5. What is the difference between Object.keys and Object.entries? Write example code using both of them.
  6. How do we get only the values of an object?
@Abdo-Mos
Copy link

Abdo-Mos commented Apr 9, 2022

  1. We can't access an item by index as in an array.
    We can’t use push while adding property.
    We access properties of an object with dot sign.
    Arrays contain values, while objects contain property and its value.

  2. we can change inside of an object (delete, push etc) but we can't change the object itself, which means if I declare an object called “eg: person” I can not declare another object called person, while I can change the content of this object.

  3. We can't use dot for arrays; dot is used for objects properties. However for example I have an array that consists
    [1,2,3,4] then we can say array.name = "zisan". After printing this array it will give [1,2,3,4], when we
    want to console log array.name it will give zisan. For the brackets, if we want to print properties of an object
    we should use string inside brackets, but if we want to print items of an array we should have index inside brackets.

  4. Computed properties allow you to dynamically choose what property in your object gets updated. It’s important to give your input element a name attribute, and name attribute that you specify should match the property that you wish to update within the object.
    E.g.

    ​​// Computed property
    let i = 0
    let a = {
    ['foo' + ++i]: i,
    ['foo' + ++i]: i,
    ['foo' + ++i]: i
    }
    console.log(a.foo1) // 1
    console.log(a.foo2) // 2
    console.log(a.foo3) // 3

  5. object.key is the value for attribute , while object.entire is a pair of attribute and it’s value
    let object = {name: "zisan", lastname:"uzum", age:22}
    undefined
    Object.keys(object)
    Array(3) [ "name", "lastname", "age" ]
    Object.entries(object)
    Array(3) [ (2) […], (2) […], (2) […] ]
    0: Array [ "name", "zisan" ]
    1: Array [ "lastname", "uzum" ]
    2: Array [ "age", 22 ]
    length: 3

  6. we can call the object with its name console.log(Object.values(obj))

Group Names: Kemal Davut , Zisan Uzum, Rasha Bsirini, Abdelrahman Mostafa

@aghimohamad
Copy link

aghimohamad commented Apr 9, 2022

group (mohamad aghi, aya mydani, sara hamoud, irem kurt)

0- Arrays store the data in an ordered collection in which the data can be accessed using a numerical index while objects store in a pair form. We must put [] to arrays and {} to objects. We access values through the index in the array while we access values through key.(object.key())

  1. when we declare an object using const we cant redeclare it again and we cant reassign it also .
    but we can make some changes to it means we can add, remove or edit keys and values.

  2. the bracket notation allows us to access object properties using different values rather than the dot that just allows us to access the specified keys also we can't use variables with dot notation.

  3. a property name that returns a value not just store it

  4. Object.keys(obj) – returns an array of keys.
    Object.values(obj) - returns an array of [key, value] pairs.
    let user = {
    name: "John",
    age: 30
    };
    Object.keys(user) = ["name", "age"]
    Object.entries(user) = [ ["name","John"], ["age",30] ]

  5. object.values(obj)

@amjadmak
Copy link

amjadmak commented Apr 9, 2022

Cengiz aksakal, Amjad maqsouma, gizem haspolat, sobhan shams, Rama aldakkak:

  1. 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.

  2. 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.

  3. 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: "johndoe@gmail.com"
};

console.log(customers.firstName);
// Output: John

const value = "firstName";

console.log(customers[value]);
// Output: John

  1. A computed property is a property that calculates and returns a value, rather than just store it.
    const customers = {
    firstName: "John",
    lastName: "Doe",
    email: "johndoe@gmail.com"
    };

console.log(customers.firstName);
// Output: John

const value = "firstName";

console.log(customers[value]);
// Output: John

  1. The Object.keys() method in JavaScript returns an array whose elements are strings corresponding to the enumerable properties
    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"

@ghufran-adel
Copy link

ghofran .nouh . mohammed . yaman
0)the object has value & key ..so it can store more detailed data.. while array store the data as a list .

1)const in object give us the possibility to change the object content .

  1. the bracket notation allows us to access object properties using variable while dot not ,for example
    obj.123; // ❌ SyntaxError
    obj['123']; // ✅ 'digit'
    because it starts with a digits and every thing that works with dot notation dose work with bracket notation.

4)when we use( Object.keys) the return value is "string" but in case (Object.entries) the return value is array that consist of a key and a value.
Object.keys example:
const object1 = {
a: 'somestring',
b: 42,
c: false
};

console.log(Object.keys(object1));
object.entries example:
const object1 = {
a: 'somestring',
b: 42
};

for (const [key, value] of Object.entries(object1)) {
console.log(${key}: ${value});
}

// expected output:
// "a: somestring"
// "b: 42"

5)console.log(Object.values(object1));

@HasanAldhahi
Copy link

HasanAldhahi commented Apr 9, 2022

Group 10
Hasan, Rama, Mohammed, and Melek
1)
Array is different type of object where order matter and it has hidden properties like an object using dot operator like "summary and length"" you can access these hidden property. Object is the preferred way to store collection of data for CRUD Data operations.
The Const keyword makes a variable itself immutable, not its assigned content meaning that Object's properties including keys and values can be changed but the data structure type of that declared Const variable can't be changed to other types.

The main difference between bracket and dot notation is that bracket notation allows us to access object properties using variables. To access JavaScript object properties using bracket notation, the key should be. Property identifiers have to be a String or a variable that references a String. Property identifiers cannot start with a number.
For the Arrays the method to access the elements is using the bracket syntax. array[0] the JavaScript engine takes the number 0 change it into string and access the element automatically where u cannot use dot operator.
For Objects, the preferred method to access the values of the keys is using the dot operator JavaScript ES5 (2015) new edition added more features such as .? for accessing objects and checking if this value exists without getting error from JavaScript.

Computed properties can be calculated outside and accessed from the inside of the object
In this example, the [Name] is a computed property of the rank object. The property name is derived from the value of the Name variable. the dynamical approach of changing the names of certain key or value inside an object can a good example to represent this phenomena, as shown below:
let Name = 'h';
const rank = {
a: 1,
b: 2,
[Name]: 3,
};
console.log(rank.c);
4)
Object.keys returns an array of keys after including the object indie the Brackets.
Object.entries it returns an array of the key and the value specifically .

you get the value of the object by using Object.Values and it will return an array of values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment