Object Methods in JavaScript: Every JavaScript developer must know.
JSON Object are always a part of any system built. Below are object methods every JavaScript must know to process these JSON Objects.
Object.freeze
Object.freeze prevents any changes to the objects passed.
const data = {
name: "Rhea",
id: 1
}
Object.freeze(data)
data.id = 2;
console.log(data) // prints {name: "Rhea", id: 1}, value will not change.
Object.keys
Object.keys returns an array containing all the keys of the Object.
const data = {
name: "Rhea",
id: 1
}
console.log(Object.keys(data)) // prints Array [ "name", "id" ]
Object.values
Object.values returns an array containing all the values of the Object.
const data = {
name: "Rhea",
id: 1
}
console.log(Object.values(data)) // prints Array [ "Rhea", 1 ]
Object.entries
Object.entries returns both the keys and its values in the form of a nested array.
const data = {
name: "Rhea",
id: 1
}
console.log(Object.entries(data)) // prints Array [[ "name", "Rhea" ], ["id", 1 ]]
Object.assign
Object.assign is used for merging two objects as one.
const data = {
name: "Rhea",
id: 1
}
const meta = {
favourite_color : "black"
}
const combined = Object.assign(data, meta)
console.log(combined) // { name: "Rhea", id: 1, favourite_color : "black"}
If you liked my blog, please follow me on Rhea RB.
Thank you for Reading ! Happy coding !!