Compare commits
4 Commits
78ebe07977
...
a5be34f3ab
Author | SHA1 | Date | |
---|---|---|---|
a5be34f3ab | |||
096d8af2ee | |||
14dbb96102 | |||
e937deceea |
12
5.4ArrayMethod/index.html
Normal file
12
5.4ArrayMethod/index.html
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Document</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script src="./t.js"></script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
51
5.4ArrayMethod/t.js
Normal file
51
5.4ArrayMethod/t.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
array = ["Bilbo", "Gandalf", "Nazgul"];
|
||||||
|
array.forEach((item, index, array) =>{
|
||||||
|
console.log(`${item} is at index ${index} in ${array}`)
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`arr.indexOf(Bilbo): ${array.indexOf("Bilbo")}`)
|
||||||
|
console.log(`arr.indexOf(Bilbo): ${array.includes("Bilbo")}`)
|
||||||
|
|
||||||
|
let fruits = ['Apple', 'Orange', 'Apple']
|
||||||
|
console.log(`fruits.indexOf('Apple'): ${fruits.indexOf('Apple')}`)
|
||||||
|
console.log(`fruits.lastIndexOf('Apple'): ${fruits.lastIndexOf('Apple')}`)
|
||||||
|
|
||||||
|
// arr.find(fn)
|
||||||
|
let users = [
|
||||||
|
{id: 1, name: "xingzhesun"},
|
||||||
|
{id: 2, name: "zhexingsun"},
|
||||||
|
{id: 3, name: "sunxingzhe"},
|
||||||
|
];
|
||||||
|
|
||||||
|
let user1 = users.find(item => item.id == 1);
|
||||||
|
let user2 = users.findLastIndex(item => item.id == 1);
|
||||||
|
console.log(user1.name);
|
||||||
|
console.log(user2.name);
|
||||||
|
|
||||||
|
// arr.filter
|
||||||
|
let someUsers = users.filter(user => user.id < 3);
|
||||||
|
console.log(someUsers)
|
||||||
|
someUsers = users.filter(function(item, index, array){
|
||||||
|
if (item.id < 3){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
console.log(someUsers);
|
||||||
|
|
||||||
|
// arr.map(fn)
|
||||||
|
let lengths = array.map(item=>item.length);
|
||||||
|
console.log(lengths);
|
||||||
|
|
||||||
|
// arr.sort(fn)
|
||||||
|
function compare(a, b){
|
||||||
|
if (a > b) return 1;
|
||||||
|
if (a == b) return 0;
|
||||||
|
if (a < b) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
arr = [15, 2 ,1];
|
||||||
|
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)))
|
30
index.html
30
index.html
@ -0,0 +1,30 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.css">
|
||||||
|
<!-- add mocha framework code -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.2.0/mocha.js"></script>
|
||||||
|
<script>
|
||||||
|
mocha.setup('bdd'); // minimal setup
|
||||||
|
</script>
|
||||||
|
<!-- add chai -->
|
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
|
||||||
|
<script>
|
||||||
|
// chai has a lot of stuff, let's make assert global
|
||||||
|
let assert = chai.assert;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
let code = {
|
||||||
|
"49": "Germany",
|
||||||
|
"+49": "Germany number",
|
||||||
|
"1": "US",
|
||||||
|
"+1": "US number"
|
||||||
|
};
|
||||||
|
for(key in code){
|
||||||
|
alert(key);
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
Loading…
Reference in New Issue
Block a user