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?
@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