Skip to content

Instantly share code, notes, and snippets.

@Kishimoto96
Forked from halitbatur/discussion.md
Created March 18, 2023 09:33
Show Gist options
  • Save Kishimoto96/23a36288eda0e766eb85456dd393d242 to your computer and use it in GitHub Desktop.
Save Kishimoto96/23a36288eda0e766eb85456dd393d242 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?
@sncey
Copy link

sncey commented Mar 18, 2023

@afrakucukaydin @MOHAMMAD-ALMOHAMMAD @Abd2023 @tomiece317
0. arrays are objects, but objects contain key values pairs, and arrays contain index based values in an ordered fashion, though arrays can also contain key value pairs. Objects are accessed using keys, while arrays are accessed using indices.

  1. const can be used to declare a variable that cannot be reassigned, but it does not make the variable immutable. When an object is declared with const, the reference to the object cannot be reassigned to a different object. However, the properties of the object can still be modified. This means that while the object itself is considered constant, its properties can still be changed.

  2. bracket syntax and dot syntax are two ways to access and manipulate object properties.Something that works with bracket syntax but not dot syntax is accessing object properties with spaces or special characters in their names. For example: myObj['my property'] would work with bracket syntax but not with dot syntax (myObj.my property is not valid). Something that works with dot syntax but not bracket syntax is accessing object properties with literal identifiers. For example: myObj.myProperty would work with dot syntax but not with bracket syntax (myObj.'myProperty' is not valid).

  3. Computed properties allow you to dynamically compute the property names in an object literal.
    const propName='gg';
    const obj={ [propName]:3507};
    console.log(obj.gg); //output will be : 3507

  4. we use Object.keys() and Object.entries() to get info about the properties in an object. Object.keys returns the property names. Object.enrties returns the entries in an array form.
    const obj={a:1, b:2, c:3};
    console.log(Object.keys(obj)); // output will be: ["a","b","c"]
    console.log(Object.entries(obj)); // output will be: [["a", 1], ["b", 2] , ["c", 3]]

  5. We can get only the values of an object by using the Object.values() method. It returns an array of values from the object.

@jimaa-maya
Copy link

Gulet, Sakarie, Jimaa
0. Array: We can store different data type in one variable while objects are used to store collections of key-value pairs with no specific order and can store values of different data types.
2. When using "Const" with Object, we cannot reassign the values of the object later, but we can reassign the properties of the object.
3. Dot notation is the more common syntax, while bracket notation is used in certain cases when the property name is not a valid identifier or is dynamic.
4. Both methods in are used to extract information about the properties of an object. The difference in is type of information they provide
5. We Can use Object.values().

@idincer944
Copy link

Members: Cansu Aydagdic, Mohamad Aid, Mustapha Nori, İsmail Dincer
0- In arrays data are indexed and in order. In arrays an element can be accesed with its index. We should use arrays for numbers. In objects there are keys and values for those keys. In objects the values can be accesed by their keys. We should use objects to store strings.
1- We can change the const object's values and keys. However, you cannot assign it to be a different object.
2- Dot notation is faster to write and easier to read than bracket notation.
However, you can use variables with bracket notation, but not with dot notation. This is especially useful for situations when you want to access a property but don’t know the name of the property ahead of time. For example, if we wanted to iterate over all the keys in an object, and access their values, we could use bracket notation.
3-
4- Object.keys returns an array of keys of an object. Object.entries returns an array of keys and values of an object.
5- We use Object.values.

@OmarQaqish
Copy link

Team members: Omar Qaqish, Nur Abunamus, Bedreddin Naser, Harith Riyadh
0. In array we are access the data through indexes, while in objects, we access the data through keys and properties.

  1. You can update the properties of the object, but we cannot assign a new object with the const var.
  2. Square bracket notation allows access to properties containing special characters and selection of properties using variables. With square bracket notation, you can access a property without knowing the name of the property. Example: for (let key in obj) { console.log(obj[key]);}
    Another example: let obj = {“foo.Bar”: 2}
    obj[“foo.Bar”] // valid syntax
    obj.foo.Bar //invalid syntax
  3. Computed Property Names allows the names of object properties in JavaScript to be determined dynamically, i.e., computed. Example:
    let propertyname = 'c'; let obj ={ a : 11, b : 12, [propertyname] : 13 };
  4. Object.keys gives us an array with all keys inside an object.
    const teamNames {
    name1 : “Nur”,
    name2 : “Omar”,
    name3 : “Bedreddin”,
    name4 : “Harith”,
    }
    Object.keys will return an array that looks like [name1, name2, name 3, name4]
    Object.entries will return an array with the keys and values.

@cyberRasam
Copy link

Room 12

Contributors :
@Silvor23
@abdulsalamhamandoush
@motaz99

0- Arrays are indexed base, and we have pair key values in objects. (Array is also a type of an object in JS). Arrays are useful when dealing with ordered data, while objects are more flexible and can be used to represent more complex data structures such as maps, dictionaries, and records.

1- Const prevents the variable from being reassigned to a new object, but it does not make the object immutable.

2- dot notation is used while we have strict properties and bracket notations are used while our property is dynamic or not a valid identifier.
const person = {
name: 'John',
age: 30
};

const propertyName = 'name';
console.log(person[propertyName]);
// dot notation exam plcosnole.log(person.name)

3- Computed properties are a way to dynamically generate object property names. They allow us to use an expression, enclosed in square brackets, to compute the property name at runtime. This is particularly useful when the property name cannot be determined until runtime.

 const prefix = 'user_';

 const user = {
    [prefix + 'name']: 'John',
    [prefix + 'age']: 30

};

console.log(user['user_name']); // Output: John
console.log(user['user_age']); // Output: 30

4- both methods in JavaScript to access the properties of an object,
Object.keys() returns an array of all the enumerable properties of an object they appear in the object. Each element of the array is a string that represents a property
example :
const person = {
name: 'John',
age: 30,
gender: 'Male'
};

const keys = Object.keys(person);
console.log(keys); // Output: ['name', 'age', 'gender']

 Object.entries() returns an array of all the enumerable properties. Each element of the array is an array containing two elements: the first element is a string that represents the property name, and the second element is the value of the property.
example : 
const person = {
          name: 'John',
          age: 30,
          gender: 'Male'
      };

const entries = Object.entries(person);
console.log(entries); // Output: [['name', 'John'], ['age', 30], ['gender', 'Male']]

5- We can use Object.values() and we give this function the object that we want to take it's value out, for example
const obj = {
a: 'Abdulsalam', b: 'Rasam', c: 'Motaz', d: 'Abdullah'}
const Values = Object.values(obj)
console.log(values)

@zehraworks
Copy link

zehraworks commented Mar 18, 2023

@AsliSema @aydinfz Mahmoud Alshahin , @lawand703

  1. Object stores key and value pairs, an array is a list. The array is a special type of object.
  2. Const means constant, we are not able to reassign any other objects to that object which is defined by const. We can change elements of it because it remains in the same place as an object in the memory.
  3. Dot notation uses only static keys but bracket syntax can use for special characters in keys and the selection of properties using variables.
  4. Computed properties allow you to use the values of expressions as property names of an object.
  5. Object.keys collect all keys in an array, Object.entries give key-value pairs in an array and whole object also in an array (nested array)
  6. const obj = {
    x: ‘hey’,
    y: 13,
    };Object.values(obj) // [‘hey’ , 13]

@HishamWattar
Copy link

HishamWattar commented Mar 18, 2023

Team members :Hisham Al Wattar , Sara Nafisa , Talal Bakkour

  1. The main differences between objects and arrays are that objects are unordered and store key-value pairs, while arrays are ordered and store elements accessed by index
  2. The const keyword makes a variable itself immutable, when using const with objects in JavaScript, the reference to the object cannot be reassigned, but the object's properties can be modified.
  3. Bracket syntax is used when the property or method name is dynamic or has special characters, while dot syntax is used when the property or method name is static and known at the time of writing the code
    Dot syntax ex :
    const student= { name: 'John', age: 20}; console.log(student['name']);
    Bracket syntax ex :
    const student= { 'first name': 'John', age: 20}; console.log(student['first name']);
  4. Computed object properties, are a feature in JavaScript that allows you to use an expression to dynamically compute the property name of an object literal , This means that the property name is not fixed, but instead is determined at runtime based on the value of an expression
    ex :
    const student= {name: 'John',age: 30,['country' + '_code']: 'us'}; console.log(person['country_code']);
  5. Both methods are used to iterate over the properties of an object, but they return different data types ,The Object.keys() returns an array of the object's own enumerable property names , The Object.entries() returns an array of a given object's own enumerable string-keyed property key-value pairs
    Object.keys() ex :
    const student= { name: 'John', age:18, city: 'Istanbul' }; const keys = Object.keys(student); console.log(keys); // output: ['name', 'age', 'city']
    Object.entries() ex :
    const student= { name: 'John', age: 18, city: 'Istanbul' }; const entries = Object.entries(student); console.log(entries); // output: [['name', 'John'], ['age', 18], ['city', 'Istanbul']]
  6. To get only the values of an object, we use the Object.values()

@nourkrimesh
Copy link

// M.NOUR Krimesh, Abdul Malek, Ammar Almuain.

  1. The difference between object and array is that object store its data as key:value and arrays store the data as indexed values.

  2. const prevent us from defining another variable with the same name but we can change object's values.

  3. Bracket notation allows you to access properties with special characters in their names, while you can not do this with dot notation

  4. Computed properties allow you to dynamically choose what property in your object gets updated.

  5. Object.keys(object) is a utility function that returns the list of keys of object for example:
    const hero = {
    name: 'Batman',
    city: 'Gotham'
    };
    Object.keys(hero); // => ['name', 'city']

    Object.entries(object) is an useful function to access the entries of object for example:
    const hero = {
    name: 'Batman',
    city: 'Gotham'
    };
    Object.entries(hero); // => [['name', 'Batman'], ['city', 'Gotham']]

  6. We get only the values of an object by using Object.values(object) for example:
    const books = {
    'book1': 5.50,
    'book2': 10.00,
    'book3': 4.35
    };
    const prices = Object.values(books);
    prices; // => [4.35, 5.5, 10]

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