Object
6 methodsStatic methods for working with objects.
Object.keys(obj)Return an array of the object's own enumerable string-keyed properties.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | object | Source object. |
Returns
string[]
Example
javascript
Object.keys({a: 1, b: 2}) // ['a', 'b']
Object.keys([1, 2, 3]) // ['0', '1', '2']Object.values(obj)Return an array of the object's own enumerable property values.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | object | Source object. |
Returns
any[]
Example
javascript
Object.values({a: 1, b: 2}) // [1, 2]
Object.values('ab') // ['a', 'b']Object.entries(obj)Return an array of [key, value] pairs of the object's own enumerable properties.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | object | Source object. |
Returns
[string, any][]
Example
javascript
Object.entries({a: 1, b: 2}) // [['a', 1], ['b', 2]]
Object.fromEntries(Object.entries({a: 1}).map(([k, v]) => [k, v * 2])) // {a: 2}Object.assign(target, ...sources)Copy all enumerable own properties from sources to target. Returns target.
Parameters
| Name | Type | Description |
|---|---|---|
| target | object | Target object (mutated). |
| sources | ...object | Source objects. |
Returns
object
Example
javascript
Object.assign({}, {a: 1}, {b: 2}) // {a: 1, b: 2}
Object.assign({a: 1}, {a: 2, b: 3}) // {a: 2, b: 3} (overwrites)Object.freeze(obj)Freeze an object: prevents adding, removing or modifying properties.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | object | Object to freeze. |
Returns
object
Example
javascript
const o = Object.freeze({a: 1});
o.a = 2 // silently fails (or throws in strict mode)
o.b = 3 // fails
// o is still {a: 1}Object.fromEntries(iterable)Transform a list of [key, value] pairs into an object.
Parameters
| Name | Type | Description |
|---|---|---|
| iterable | Iterable<[key, value]> | Iterable of key-value pairs. |
Returns
object
Example
javascript
Object.fromEntries([['a', 1], ['b', 2]]) // {a: 1, b: 2}
Object.fromEntries(new Map([['k', 'v']])) // {k: 'v'}