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?
@berfinkaraman
Copy link

berfinkaraman commented Apr 9, 2022

We are the people of group 5 (Dilara Katuk, Dilara Fırtına, Abdul Hafiz Mhammadah, Berfin Karaman), and here are our answers:
0. The main difference is the item storing. Iterating through values is different, for example, we can use "for" loops directly for arrays while we would need additional functions to use "for" loops.

  1. Variable that holds "const", that cannot be redeclared. We can reassign the values for const working with objects.
  2. You cannot use the dot syntax with strings that have spaces or capital letters as the initial character. Eg. let customerName = "abc";
    customer[customerName];
    //=> "abc"
    customerName= "abc";
    customers.customerName;
    //=> undefined
  3. Computed Property Names is a feature that allows the names of object properties in JavaScript object literal notation to be determined dynamically.
    const myPropertyName = 'c'

const myObject = {
a: 5,
b: 10,
[myPropertyName]: 15
}

console.log(myObject.c) // prints 15
4. Object.key returns just the keys while object.entries return the keys and the values.
const object1 = {
student1: 'Dilara',
student2: 'Dilara 2',
student3: 'Berfin',
student4: 'Abdul'
};

console.log(Object.keys(object1));
// expected output: Array ["Dilara", "Dilara 2", "Berfin", "Abdul"]
console.log(Object.enteries(object1));
// expected output:
// "student1: Dilara"
// "student2: Dilara 2"
// "student3: Berfin"
// "student4: Abdul"

  1. Object.value()

@ozgeerdemir
Copy link

Group Members: Beyza Fatma Bostancı, Hamza Aymen, Özge Erdemir

1.In JS, almost everything is an object. Arrays are more like a list. Objects store a collection of data. Objects represent a special data type. Arrays are a special type of variable.

2.The const keyword makes a variable itself immutable, not its assigned content. When the content is an object, this means the object itself can still be altered. Therefore, it's possible to change the content of the object that is declared with const variable, but you cannot assign a new object to a a const variable.

3.Bracket syntax allows us to access object properties using variable. When working with dot notation, property identifies can only be alphanumeric (and _ and $). Properties can’t start with a number.

Dot notation:
Property identifies can only be alphanumeric (and _ and $)
Property identifiers cannot start with a number.
Property identifiers cannot contain variables.
OK — obj.prop_1, obj.prop$
Not OK — obj.1prop, obj.prop name
Bracket notation:
Property identifiers have to be a String or a variable that references a String.
It is okay to use variables, spaces, and Strings that start with numbers
OK — obj["1prop"], obj["prop name"]

When working with bracket notation, property identifiers only have to be a String. They can include any characters, including spaces. Variables may also be used as long as the variable resolves to a String.

EXAMPLE:

let obj = {
cat: 'meow',
dog: 'woof'
};
let dog = 'cat';
let sound = obj[dog];
console.log(sound);
// meow

4.A computed property is a property that calculates and returns a value, rather than just store it

5.Object.keys() return an array of keys only , while Object.entries() return an array of key value pairs

code example :
let x = {
name:adam,
age:21
}
Object.keys(x) => ['name','age'];
Object.entiers(x) => [[‘name','adam'],['age',21]];

  1. Object.values()

@okurezgi
Copy link

okurezgi commented Apr 9, 2022

  1. Object is unordered but array is ordered. Object has distinct key for each value but array has index. Objects are created with {}, arrays [].
  2. const gives us the opportunity to change the context of the object but not the object itself.
  3. If a key has blanks in it or is a number you can't use dot notation. We can use bracket for every situation. Example: 'const m={
    0:"1",
    'hello world' : 'hi'
    computed : 2=+2
    }
    console.log(m.0) - That does not work with dot notation, but works with bracket
    console.log(m.hello world) - That does not work with dot notation, but works with bracket
  4. The property that perform computation in it. It allows us to use an expressions in brackets. Example: 'let propName = 'c';
    const rank = {
    a: 1,
    b: 2,
    [propName]: 3,
    };
    console.log(rank.c); // 3
    4.Object.keys gives us the keys of the object, object.entries gives us the key values. Example:
    const m={
    first:"1",
    }
    obj=Object.entries(m)
    console.log(obj)
    keyss=Object.keys(m)
    console.log(keyss)
    5.Example:
    const m={
    0:"1"
    }
    values=Object.values(m)
    console.log(values)
    Ezgi , Mehmet Baki, Berk Akipek, Hani Muhammed.

@Keen91
Copy link

Keen91 commented Apr 9, 2022

Team Members: Mohammad Sheikh Ibrahim, Mustafa Arslan, Israa Qaba, and Kinan Hatahet.
0. Objects have keys and values unlike arrays. Objects keys are more descriptive index than number in the arrays.

  1. With const, we can change the content of the object but we can't change the object itself.
  2. Bracket syntax is more practical to use. With it, you can access keys or values that include spaces and dots unlike in dot syntax. Also, with bracket syntax we can access to variables.
    e.g. ar.var_namr wouldn't work , but ar["var_name"] works
  3. Computed properties are the ability to use the [ ] instead of the dot notation in order to reach a a key inside the object.
    e.g. const person ={
    name:"aname",
    surname:"aasd"
    }

const here="name"
console.log(person[here])

  1. With Object.keys, we can return only the keys. However, object entries return a pair of the key and the value.
    e.g.
    const person ={
    name:"aname",
    surname:"aasd"
    }
    console.log(Object.keys(person)) >> ["name","surname"]
    console.log(Object.entries(person)) >> [["name","Aname"],["surname","assd"]]

  2. with Object.values.

@muhammed-shihebi-boot
Copy link

muhammed-shihebi-boot commented Apr 9, 2022

  • Team members:
    • Adnan Khaldar
    • Amjad Alkhatib
    • Öznur Ergen
    • Muhammed Şihebi
  1. How is an object different from an array?

    • Array
      • Values are organized as a list of items without any key to access them.
      • We can access a value using an index.
    • Object
      • Properties are organized using a key-value pair.
      • Properties are not ordered.
  2. How does const work with objects?

    • Const for an object prevents us from instantiating another object with the same name. However, we can still modify/add/delete properties inside of the object.
  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.

    • Dot syntax
      • We can’t use the dot syntax with non-standard (ex. “first name”) keys.
      • With this notation, we don’t have to use quotation marks to access the value.
    • Bracket syntax
      • We can use the bracket syntax with both standard (ex. firstName) and non-standard keys.
  4. What are computed properties? Write an example code.

    • Using a computed property, instead of supplying a key directly, we can use an expression as a key.
    let propName = 25 + 10;
    
    const rank = {
      a: 1,
      b: 2,
      [propName]: 3,
    };
    
    console.log(rank[35]);
  5. What is the difference between Object.keys and Object.entries? Write example code using both of them.

    • Object.keys: returns an array of keys.
    • Object.entries: returns an array of arrays. Each one of the inner arrays contains 2 values, the first one is the key, and the second one is the value.
    const object1 = {
      a: 'somestring',
      b: 42
    };
    
    console.log(Object.entries(object1)); // [ [ 'a', 'somestring' ], [ 'b', 42 ] ]
    console.log(Object.keys(object1)); // [ 'a', 'b' ]
  6. How do we get only the values of an object?

    Object.values(obj)

@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