object Method
let westeros = {
cersei: 'Lannister',
arya: 'Stark',
jon: 'Snow',
brienne: 'Tarth',
daenerys: 'Targaryen',
theon: 'Greyjoy',
jorah: 'Mormont',
margaery: 'Tyrell',
sandor: 'Clegane',
samwell: 'Tarly',
ramsay: 'Bolton'
}
//for(let prop of arr)
// foreach( ) filter( ) map() reduce()
let keys = Object.keys(westeros);
console.log('Keys ', keys);
let vals = Object.values(westeros);
console.log('Vals', vals);
let entries = Object.entries(westeros);
console.log('Entries', entries);
console.log(entries[2][1]);
JavaScript Nested Loops with Arrays and Objects
// nested loops and multi-dimensional objects
// We can use nested loops to access all the elements
// inside multi-dimensional arrays or objects
let twoD = [
[1, 2, 3, 4, 5, 6, 7],
[8, 10, 5, 7, 3, 22, 6, 42],
[123, 54, 12, 11, 9, 15]
];
let bigHero = {
characters: [{
name: 'Hiro',
voice: 'Ryan Potter'
},
{
name: 'Baymax',
voice: 'Scott Adsit',
prop: 12
},
{
name: 'Go Go Tamago',
voice: 'Jamie Chung'
},
{
name: 'Fred',
voice: 'T.J. Miller'
}
]
};
let chars = bigHero['characters']; //bigHero.characters
for (let i = 0, len = chars.length; i < len; i++) {
// console.log(chars[i]);
console.log(chars[i].name);
console.log(chars[i]['voice']);
for (let prop in chars[i]) {
console.log(prop, chars[i].prop, chars[i][prop]);
}
}
// nested for loops
let rows = twoD.length;
for(let i=0; i<rows; i++){
let items = twoD[i].length;
// console.log(i, items)
for(let n=0; n<items; n++){
console.log( twoD[i][n] );
}
}
Destructuring with Promises and Array Methods
Destructuring allows us to see inside an object or array when it is being passed to a function. This can be combined with calls to Array methods and Promise.then methods to great effect.
/*
Destructuring allows us to see inside an object
or array when it is being passed to a function.
This can be combined with calls to Array methods
and Promise.then methods to great effect.
*/
const log = console.log;
let people = [{
id: 1,
name: 'Leonard',
phd: true,
partner: 'Penny'
},
{
id: 2,
name: 'Howard',
phd: false,
partner: 'Bernadette'
},
{
id: 3,
name: 'Sheldon',
phd: true,
partner: 'Amy'
},
{
id: 4,
name: 'Raj',
phd: true,
partner: 'Cinnamon'
},
];
let nums = [12, 34, 56, 78, 90];
let n = nums.map((number) => {
log(number);
});
let ppl = people.map(({
name: nm,
partner: pt
}) => {
log(nm, '&', pt);
});
fetch('./people.json')
.then((resp) => {
if (!resp.ok) throw new Error(resp.statusText);
return resp.json();
})
.then(([first, second, ...rest]) => {
//...rest MUST be the last argument for desctructuring
// rest[rest.length-1] would be the last element.
log(first);
log(second);
})
.catch(log);
String Method
/**
* String.prototype.startsWith(str [,pos]) //returns Boolean
* String.prototype.endsWith(str [,pos]) //returns Boolean
*
* String.prototype.includes(str [,pos]) //returns Boolean
* String.prototype.indexOf(str [,pos]) //returns -1 or position index
* String.prototype.lastIndexOf(str [,pos]) //returns -1 or position index
* String.prototype.charAt(pos) //returns the single code point at that position
* String.prototype.match(regExp); //returns an Array of matches or null. regex.test() and regex.exec()
*/
let log = console.log;
let sentence = 'How are you today?👋🏻';
let len = sentence.length;
let regexp = /[Ho]/g;
// log(sentence.indexOf('👋🏻')); //18
// log(sentence.lastIndexOf('👋🏻')); //18
// log(sentence.includes('x')); //false
// log(sentence.match(regexp));// [] or null
// log(regexp.test(sentence)); // true
// log(sentence.charAt(0) === 'H');
// log(sentence.charAt(18));
// log(sentence.substring(18));
log(sentence.startsWith('H'));
log(sentence.startsWith('h'));
log(sentence.startsWith('👋🏻', 18));
log(sentence.endsWith('👋🏻'));
watermark.image?)