MOCKSTACKS
EN
Questions And Answers

More Tutorials









Javascript Objects

ES6's Object.assign() function can be used to copy all of the enumerable properties from an existing Objecct instance to a new one.


const existing = { a: 1, b: 2, c: 3 };
const clone = Object.assign({}, existing);

This includes Symbol properties in addition to String ones.


Object rest/spread destructuring which is currently a stage 3 proposal provides an even simpler way to create shallow clones of Object instances:


const existing = { a: 1, b: 2, c: 3 };
const { ...clone } = existing;

If you need to support older versions of JavaScript, the most-compatible way to clone an Object is by manuallly iterating over its properties and filtering out inherited ones using .hasOwnProperty().


var existing = { a: 1, b: 2, c: 3 };
var clone = {};
for (var prop in existing) {
 if (existing.hasOwnProperty(prop)) {
 clone[prop] = existing[prop];
 }
}

Object.freeze

Object.freeze makes an object immutable by preventing the addition of new properties, the removal of existing properties, and the modification of the enumerability, configurability, and writability of existing properties. It also prevents the value of existing properties from being changed. However, it does not work recursively which means that child objects are not automatically frozen and are subject to change.
The operations following the freeze will fail silently unless the code is running in strict mode. If the code is in strict mode, a TypeError will be thrown.


var obj = {
 foo: 'foo',
 bar: [1, 2, 3],
 baz: {
 foo: 'nested-foo'
 }
};
Object.freeze(obj);
// Cannot add new properties
obj.newProperty = true;
// Cannot modify existing values or their descriptors
obj.foo = 'not foo';
Object.defineProperty(obj, 'foo', {
 writable: true
});
// Cannot delete existing properties
delete obj.foo;
// Nested objects are not frozen
obj.bar.push(4);
obj.baz.foo = 'new foo';

Convert object's values to array

var obj = {
 a: "hello",
 b: "this is",
 c: "javascript!",
};

You can convert its values to an array by doing:


var array = Object.keys(obj)
 .map(function(key) {
 return obj[key];
 });
console.log(array);

Output

["hello", "this is", "javascript!"]

Object.getOwnPropertyDescriptor

Get the description of a specific property in an object


var sampleObject = {
 hello: 'world'
};
Object.getOwnPropertyDescriptor(sampleObject, 'hello');

Output

Object {value: "world", writable: true, enumerable: true, configurable: true}

Object.keys

Object.keys(obj) returns an array of a given object's keys.


var obj = {
 a: "hello",
 b: "this is",
 c: "javascript!"
};
var keys = Object.keys(obj);
console.log(keys); 

Output

["a", "b", "c"]

Object.values()

The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).


var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.values(obj));

Output

['a', 'b', 'c']


Conclusion

In this page (written and validated by ) you learned about Javascript Objects . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Arithmetic.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.