Objects

Mayank KumarMayank Kumar
1 min read

Declaration ⬇️

const obj = {
  name: "Mayank",
  cls: 12,
  section: "A",
};
console.log(obj.cls);  // 12

Object.keys(obj) = key in Array (enumerable only)

Object.values(obj) = value in Array (enumerable only)

Object.getOwnPropertyNames(obj) = key in array (all)

Object.assign({}, objN, obj) = data merging, obj has priority

Object.create(obj) = use obj to create as template (inherit prop)

Object.entries(obj) = get [[key, value], [key, value]]

Object.fromEntries(obj) = convert to object

Object.seal(obj) // Object.isSealed(obj) = only Value can be change,

Object.freeze(obj) // Object.isFrozen(obj) = Disable all changes

Object.preventExtensions(obj) // Object.isExtensible = check if it can extend

Object.defineProperties = ⬇️ // writable, configurable, enumerable

const obj = {};
Object.defineProperties(obj, {
  name: { value: 'Alice', writable: true },
  age: { value: 30, writable: false }
});

Object.is(x, y) = strictly comparison (Boolean)

Object.hasOwn(obj, prop) = check if it has its own prop

Object.getOwnPropertyDescriptor(obj, prop) = check property of the prop

Object.setPrototypeOf(to, from) = inherit prop from, to and object

Object.groupBy(obj, ({key}) => key) = sort multiple obj on basis of specific key

0
Subscribe to my newsletter

Read articles from Mayank Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mayank Kumar
Mayank Kumar