What are different method of object constructor?

What are different method of object constructor?

·

2 min read

This short post will discuss with you about what are different methods of object constructor. There are generally 4 methods of it we'll discuss about them.


Methods of Object Constructor

  • Object.keys(obj) - Returns an array of keys
  • Object.values(obj - Returns an array of values
  • Object.entries(obj) -Returns an array of [key, value] pairs
  • Object.fromEntries() - Returns a new object from itrable of [key, value] pairs.

Object.keys(obj)

This method takes an object as an argument and returns an array of the object's own enumerable string-keyed property pairs in the form of [jey, value]

const obj = {
    one: 1,
    two: 2, 
    three: 3
}; 

console.log(Object.entries(obj)); 

// -> [["one", 1], ["two", 2], ["three", 3]]

Object.fromEntries()

This method is simply the reverse of Object.entries() - This static method allows you to easily transform a list of key-value pairs into an object.

const myArray = 
[['one', 1], ['two', 2], ['three', 3]]; 
const obj = Object.fromEntries(myArray); 
console.log(obj); 
// => {one: 1, two: 2, three: 3}

Object.values()

This method takes an object as an argument and returns an array of values.

const obj = {
    one: 1, 
    two: 2, 
    three: 3
}; 
console.log(Object.values(obj)); 
// => [1,2,3]

Object.keys()

This method takes an object as an argument an returns an array of keys.

const obj = {
    one: 1, 
    two: 2, 
    three: 3
}; 

console.log(Object.keys(obj)); 
// => ["one","two","three"]

Removal.AI - [SPONSOR]

Remove background from multiple images in a single upload straight to your desktop using Bulk Photo Background Remover for Windows.

  • ✅ Drag & Drop Multiple Images
  • ✅ Optimize Product Images
  • ✅ Select Your Background
  • ✅ Set Your Output Size
  • ✅ Exceptional Results

Removal AI

Visit -> Removal AI

Did you find this article valuable?

Support Rahul by becoming a sponsor. Any amount is appreciated!