var expense = {
type : 'Travel',
amount: '1800 NZD'
};
// var type = expense.type;
// var amount = expense.amount;
//console.log(type , amount);
// a better way of doing
const {type, amount } = expense;
//const {amount} = expense;
console.log(type , amount);
Another example
var savedFile = {
extension: 'jpeg',
name : 'ES 6',
size : 900
};
function fileSummary(file){
return `The file ${file.name}.${file.extension} is of size ${file.size}`;
}
console.log(fileSummary(savedFile));
// Not sure whether this is a better way or not.
//Anyways ...
function fileSummary1({name,extension,size},{domain}){
return `The ${domain}file ${name}.${extension} is of size ${size}`;
}
console.log(fileSummary1(savedFile, {domain:'fasteningcode.com'}));
Another one
const companies = [
{name: 'Google', location:'Auckland'},
{name: 'Facebook', location:'Christ church'},
{name: 'Uber', location:'Wellington'}
];
//To get the location of the first company
// var location = companies[0].location;
// console.log(location);
//better way of doing it
const [{location}] = companies;
console.log(location);
Another one
const Google = {
locations:['Auckland','Christchurch', 'Wellington']
}
//const {locations} = Google;
//console.log(locations); // Output [ 'Auckland', 'Christchurch', 'Wellington' ]
const {locations : [location]} = Google;
console.log(location); //output - Auckland
Another one from DJ Khaled
//Another example to create an object with key and value pair
const points = [
[3,6],
[2,5],
[1,8]
];
let map = points.map(([x,y])=>{
return {x,y};
});
console.log(map);