Of course. When calling object literals, you have to use brackets outside the spread operator like console.log({...personalDeets});
. And you may wonder why brackets {}
is necessary for object literals and not array. When using object literals, you have attributes that describe the data (also referred to as enumerable properties). In this case, enumerable property is: name, location
By combining brackets with spread operator, you can also merge object literals or simply add more properties like.
console.log({...personaldeets, date: "04/05/2018"});
// { name: 'Yoga', location: 'Tmn Yarl', date: '04/05/2018' }
You can also use for...in
to iterate over the enumerable properties of an object literal. This is recommended.
for(key in personalDeets){ console.log(key);}
// name
// location
If you want the values only.
for(key in personalDeets){console.log(personalDeets[key]);}
// Yoga
// Tmn Yarl
On the other hand, if you only want to show the object literals, then you don’t need to do this console.log({...personalDeets})
, you can just do console.log(personalDeets)
. However, if you want to merge object literals or add new properties, then use brackets.