DEV Community

Abddelrahman Saad
Abddelrahman Saad

Posted on

6 Useful Methods For Javascript Objects ✨

Hello guys, today we're going to explore the most used object methods with javascript.

let's div deep directly.

1-Object.values()

return array of the object's values

assume we've got this object and we need to get the values only.

  const car = {name:'Audi', model:'a4', year:2020}
  const values = Object.values(car)
  console.log(values)
  //output ['Audi', 'a4', 2020]
Enter fullscreen mode Exit fullscreen mode

2-Object.keys()

it's the opposite of the first function it return array of the object's Keys(names)

  const car = {name:'Audi', model:'a4', year:2020}
  const keys = Object.keys(car)
  console.log(keys)
  //output ['name', 'model', year]
Enter fullscreen mode Exit fullscreen mode

easy, right?

3-Object.assign()

it's Useful for merging or cloning objects

  const car = {name:'Audi', model:'a4', year:2020}
  const details = {color:'red', type:'Coupe', year:2021}


  const combined = Object.assign({},car,details)
  console.log(combined)
  //output {name:'Audi', model:'a4', color:'red', type:'Coupe', year:2021}
Enter fullscreen mode Exit fullscreen mode

Note Here: if both objects have the same property, it will take the value of the second object

4-Object.entries()

return array for each key:value pairs in object wrapped in array

  const car = {name:'Audi', model:'a4', year:2020}
  const items= Object.entries(car)
  console.log(items)
  //output [ [name:'Audi'], [model:'a4'], [year:2020] ]
Enter fullscreen mode Exit fullscreen mode

5-Object.freeze()

it makes the object no longer able to change

  const car = {name:'Audi', model:'a4', year:2020}
  Object.freeze(car)

  car.year = 2021
  console.log(car)
  //output [ [name:'Audi'], [model:'a4'], [year:2020] 

Enter fullscreen mode Exit fullscreen mode

Note Here: to check if the object is frozen or not use Object.isFrozen(car), if frozen it will return true, and false if not

6-Object.seal()

Similar to Object.freeze() but the difference is this lets you change the property of the object as long they are writeable (Not methods) But not delete or add new

  const car = {name:'Audi', model:'a4', year:2020}
  Object.seal(car)

  //this will work
  car.year = 2021
  console.log(car.year) // output 2021

  //this will Not work
  delete car.year
  console.log(car.year) // output 2021
Enter fullscreen mode Exit fullscreen mode

Note Here: to check if the object is sealed or not use Object.isSealed(car), if sealed it will return true, and false if not.

That's it for today, I hope this was useful to you.
Thank you, and have a productive day❤️

Top comments (3)

Collapse
 
kimsean profile image
thedevkim

Does Object.freeze() applies on binded states ? Thanks for sharing!

Collapse
 
ytrkptl profile image
Yatrik Patel

Well, I never heard of freeze, frozen, or seal before. Thanks for sharing.

Collapse
 
muraliprasanth profile image
MuraliPrasanth2

Object.entries(car) would be like below.
//output [ ['name', 'Audi'], ['model', 'a4'], ['year', 2020] ]
and not like the below
//output [ [name:'Audi'], [model:'a4'], [year:2020] ]