Important Object Methods
Important Object Methods
let product = {
name: "Iphone 14 Pro",
company: "Apple",
price: 125000,
warranty: "1 Year",
color: "Black"
}
Considering the fact it is an object, it will be having a lot of key value pairs. All the keys inside
an object are always unique, we cannot have duplicate keys in an object.
Object.keys()
If we want that, we need fetch all the unique keys of our object say product, then we can use
the inbuilt Object.keys method.
This method takes one argument which is the object whose keys are expected to be fetched.
Object.keys(product);
Object.values()
If we want that, we need to fetch all the available values (values can be non unique) of our
object, then we can use the inbuilt method Object.values .
Object.values(product);
It returns an array of strings, where each string represents a value corresponding to some key
in the object.
Object.entries
If instead of just fetching the keys or values separately, we want to fetch the complete pair of
(key, value), then we can use the Object.entries method. This method will take one
argument which is the object , and returns an array where each index contains a 2 length array
whose 0th index is the key and 1st index is the value.
Object.entries(product);
[
['name', 'Iphone 14 Pro'],
['company', 'Apple'],
['price', 125000],
['warranty', '1 Year'],
['color', 'Black']
]