Compare commits
3 Commits
a5be34f3ab
...
d9bba74979
Author | SHA1 | Date | |
---|---|---|---|
d9bba74979 | |||
ad51aa4fb9 | |||
3196f70784 |
@ -48,4 +48,54 @@ console.log(`arr.sort(): ${arr.sort(compare)}`);
|
||||
console.log(`arr.sort(): ${arr.sort((a,b) => a - b)}`);
|
||||
|
||||
let countries = ['Österreich', 'Andorra', 'Vietnam'];
|
||||
console.log(countries.sort((a,b) => a.localeCompare(b)))
|
||||
console.log(countries.sort((a,b) => a.localeCompare(b)))
|
||||
|
||||
// arr.reverse()
|
||||
countries = countries.reverse()
|
||||
console.log(countries)
|
||||
|
||||
// arr.split() and arr.join()
|
||||
let names = "Bilbo, Gandalf, Nazgul";
|
||||
|
||||
let nameLists = names.split(', ');
|
||||
|
||||
for(let name of nameLists){
|
||||
console.log(`A message to ${name}`);
|
||||
}
|
||||
// [LOG - 6/16/24 21:32:14] "A message to Bilbo"
|
||||
// [LOG - 6/16/24 21:32:14] "A message to Gandalf"
|
||||
// [LOG - 6/16/24 21:32:14] "A message to Nazgul"
|
||||
|
||||
nameLists = names.split(', ', 2);
|
||||
for(let name of nameLists){
|
||||
console.log(`A message to ${name}`);
|
||||
}
|
||||
// [LOG - 6/16/24 21:32:14] "A message to Bilbo"
|
||||
// [LOG - 6/16/24 21:32:14] "A message to Gandalf"
|
||||
|
||||
console.log(`nameLists: ${nameLists.join('; ')}`)
|
||||
// [LOG - 6/16/24 21:33:45] "nameLists: Bilbo; Gandalf"
|
||||
|
||||
|
||||
// arr.reduce(fn) arr.reduceRight(fn)
|
||||
let numbers = [1,2,3,4,5]
|
||||
console.log(numbers.reduce(function(sum, current){
|
||||
return sum + current;
|
||||
}, 0));
|
||||
console.log(numbers.reduce((sum, cur) => sum + cur, 0));
|
||||
console.log(numbers.reduceRight((sum, cur) => sum + cur, 0));
|
||||
|
||||
// Array.isArray()
|
||||
console.log(Array.isArray(countries))
|
||||
|
||||
function camelize(str){
|
||||
words = str.split('-')
|
||||
for(let i = 1; i < words.length; i++){
|
||||
words[i][0] = words[i][0].toUpperCase();
|
||||
console.log(words[i])
|
||||
}
|
||||
return words.join('');
|
||||
}
|
||||
console.log(camelize("background-color"))
|
||||
console.log(camelize("list-style-image"))
|
||||
console.log(camelize("-webkit-translation"))
|
55
5.5Iterables/t.js
Normal file
55
5.5Iterables/t.js
Normal file
@ -0,0 +1,55 @@
|
||||
let range = {
|
||||
from: 1,
|
||||
to: 5
|
||||
}
|
||||
|
||||
range[Symbol.iterator] = function(){
|
||||
|
||||
return {
|
||||
current: this.from,
|
||||
last: this.to,
|
||||
|
||||
next(){
|
||||
if(this.current <= this.last){
|
||||
return {done: false, value: this.current++};
|
||||
} else {
|
||||
return {done: true};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
for(let num of range){
|
||||
console.log(num);
|
||||
}
|
||||
|
||||
for(let ch of "tests"){
|
||||
console.log(ch)
|
||||
}
|
||||
|
||||
let arrayLike = {
|
||||
0: "Hello",
|
||||
1: "World",
|
||||
length: 2
|
||||
};
|
||||
|
||||
// Array.from
|
||||
let arr = Array.from(arrayLike);
|
||||
console.log(arr.pop()); // World
|
||||
arr = Array.from(range);
|
||||
console.log(arr); //1, 2, 3, 4, 5
|
||||
arr = Array.from(range, num => num * num);
|
||||
console.log(arr); //1, 4, 9, 16, 25
|
||||
|
||||
let s = "split";
|
||||
let split_s = s.split('');
|
||||
let arr_s = Array.from(s);
|
||||
console.log(split_s);
|
||||
console.log(split_s[0]);
|
||||
console.log(split_s[1]);
|
||||
console.log(split_s.length);
|
||||
console.log(arr_s);
|
||||
console.log(arr_s[0]);
|
||||
console.log(arr_s[1]);
|
||||
console.log(arr_s.length);
|
Loading…
Reference in New Issue
Block a user