add(server.tests.test2): 添加实验2的内容,将pdf里面标重要的进行提取出来
This commit is contained in:
12
Server/tests/test2/important/every.js
Normal file
12
Server/tests/test2/important/every.js
Normal file
@@ -0,0 +1,12 @@
|
||||
// every(callbackFn)
|
||||
|
||||
const stus = [{ name: '张三', score: 59 }, { name: '老钟', score: 95 }];
|
||||
let rst = stus.every((item) => {
|
||||
return item.score >= 60
|
||||
})
|
||||
console.log(rst)
|
||||
|
||||
/*
|
||||
# node every.js
|
||||
false
|
||||
*/
|
||||
25
Server/tests/test2/important/filter.js
Normal file
25
Server/tests/test2/important/filter.js
Normal file
@@ -0,0 +1,25 @@
|
||||
// filter(callbackFn)
|
||||
|
||||
const stus = [
|
||||
{ name: '张三', score: 59 },
|
||||
{ name: '老钟', score: 95 },
|
||||
{ name: 'SB', score: 80 }
|
||||
];
|
||||
|
||||
// 筛选所有 score >= 60 的信息
|
||||
const rst = stus.filter(item => { return item.score >= 60 })
|
||||
console.log(rst)
|
||||
|
||||
// 浅表拷贝,rst[0]的修改,会导致stus中对应数据的修改
|
||||
rst[0].score = 100
|
||||
console.log(stus)
|
||||
|
||||
/*
|
||||
# node filter.js
|
||||
[ { name: '老钟', score: 95 }, { name: 'SB', score: 80 } ]
|
||||
[
|
||||
{ name: '张三', score: 59 },
|
||||
{ name: '老钟', score: 100 },
|
||||
{ name: 'SB', score: 80 }
|
||||
]
|
||||
*/
|
||||
25
Server/tests/test2/important/find.js
Normal file
25
Server/tests/test2/important/find.js
Normal file
@@ -0,0 +1,25 @@
|
||||
// find(callbackFn)
|
||||
|
||||
const stus = [
|
||||
{ name: '张三', score: 59 },
|
||||
{ name: '老钟', score: 95 },
|
||||
{ name: 'SB', score: 80 }
|
||||
];
|
||||
|
||||
// 筛选第一个 score >= 60 的信息
|
||||
const rst = stus.find(item => { return item.score >= 60 })
|
||||
console.log(rst)
|
||||
|
||||
// 浅表拷贝,rst的修改,会导致stus中对应数据的修改
|
||||
rst.score = 100
|
||||
console.log(stus)
|
||||
|
||||
/*
|
||||
# node find.js
|
||||
{ name: '老钟', score: 95 }
|
||||
[
|
||||
{ name: '张三', score: 59 },
|
||||
{ name: '老钟', score: 100 },
|
||||
{ name: 'SB', score: 80 }
|
||||
]
|
||||
*/
|
||||
16
Server/tests/test2/important/findIndex.js
Normal file
16
Server/tests/test2/important/findIndex.js
Normal file
@@ -0,0 +1,16 @@
|
||||
// findIndex(callbackFn)
|
||||
|
||||
const stus = [
|
||||
{ name: '张三', score: 59 },
|
||||
{ name: '老钟', score: 95 },
|
||||
{ name: 'SB', score: 80 }
|
||||
];
|
||||
|
||||
// 返回第1个满足 score >= 60 条件的索引
|
||||
const rst = stus.findIndex(item => { return item.score >= 60 })
|
||||
console.log(rst)
|
||||
|
||||
/*
|
||||
# node findIndex.js
|
||||
1
|
||||
*/
|
||||
55
Server/tests/test2/important/reduce.js
Normal file
55
Server/tests/test2/important/reduce.js
Normal file
@@ -0,0 +1,55 @@
|
||||
// arr.reduce(
|
||||
// callback(accumulator, currentValue[, currentIndex[, array]])
|
||||
// [, initialValue]
|
||||
// )
|
||||
|
||||
const data1 = [1, 2, 3, 4, 5];
|
||||
// 没有使用初始值,则 sum 被初始为 1,cur从2开始, ..., ...
|
||||
let rst1 = data1.reduce((sum, cur) => {
|
||||
return sum += cur
|
||||
})
|
||||
console.log(rst1)
|
||||
|
||||
const data2 = [1, 2, 3, 4, 5];
|
||||
// 为初始值【1】,则temp被初始为【1】,cur从1开始, ..., ...
|
||||
let rst2 = data2.reduce((temp, cur) => {
|
||||
return temp *= cur
|
||||
}, 1)
|
||||
console.log(rst2);
|
||||
|
||||
const fruits = ['apple', 'banana', 'apple', 'banana', 'apple'];
|
||||
const rst3 = fruits.reduce((acc, cur) => {
|
||||
if (!acc[cur])
|
||||
acc[cur] = 1;
|
||||
else
|
||||
acc[cur]++;
|
||||
return acc;
|
||||
}, {});
|
||||
console.log(rst3);
|
||||
|
||||
const people = [
|
||||
{ name: 'zs', age: 25 },
|
||||
{ name: 'ls', age: 30 },
|
||||
{ name: 'ww', age: 25 },
|
||||
{ name: 'sb', age: 30 }
|
||||
];
|
||||
// 按年龄进行汇总,acc初始为{},cur从zs这条数据开始
|
||||
const rst4 = people.reduce((acc, cur) => {
|
||||
const age = cur.age;
|
||||
if (!acc[age])
|
||||
acc[age] = [];
|
||||
acc[age].push(cur);
|
||||
return acc;
|
||||
}, {});
|
||||
console.log(rst4);
|
||||
|
||||
/*
|
||||
# node reduce.js
|
||||
15
|
||||
120
|
||||
{ apple: 3, banana: 2 }
|
||||
{
|
||||
'25': [ { name: 'zs', age: 25 }, { name: 'ww', age: 25 } ],
|
||||
'30': [ { name: 'ls', age: 30 }, { name: 'sb', age: 30 } ]
|
||||
}
|
||||
*/
|
||||
14
Server/tests/test2/important/slice.js
Normal file
14
Server/tests/test2/important/slice.js
Normal file
@@ -0,0 +1,14 @@
|
||||
// let rst = array.slice([start[, end]]);
|
||||
|
||||
const animals = ["ant", "bison", "camel", "duck", "elephant"];
|
||||
|
||||
console.log(animals.slice(2))
|
||||
console.log(animals.slice(-2))
|
||||
console.log(animals.slice(2, 4))
|
||||
|
||||
/*
|
||||
# node slice.js
|
||||
[ 'camel', 'duck', 'elephant' ]
|
||||
[ 'duck', 'elephant' ]
|
||||
[ 'camel', 'duck' ]
|
||||
*/
|
||||
15
Server/tests/test2/important/some.js
Normal file
15
Server/tests/test2/important/some.js
Normal file
@@ -0,0 +1,15 @@
|
||||
// some(callbackFn)
|
||||
|
||||
const stus = [{ name: '张三', score: 59 }, { name: '老钟', score: 95 }];
|
||||
let rst = stus.some((item) => {
|
||||
return item.score < 60
|
||||
})
|
||||
|
||||
// 是否存在不及格的学生
|
||||
console.log(rst)
|
||||
|
||||
|
||||
/*
|
||||
# node some.js
|
||||
true
|
||||
*/
|
||||
55
Server/tests/test2/important/sort.js
Normal file
55
Server/tests/test2/important/sort.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const data1 = [1, 30, 4, 20, 10]
|
||||
data1.sort()
|
||||
console.log(data1)
|
||||
|
||||
// arr.sort(
|
||||
// function (a, b) {...}
|
||||
// )
|
||||
|
||||
const data2 = [1, 30, 4, 20, 10]
|
||||
// 升序排列
|
||||
data2.sort((a, b) => a - b)
|
||||
console.log(data2)
|
||||
|
||||
const data3 = [1, 30, 4, 20, 10]
|
||||
// 降序排列
|
||||
data3.sort((a, b) => b - a)
|
||||
console.log(data3)
|
||||
|
||||
const data4 = [
|
||||
{ name: 'zs', age: 28, score: 90 },
|
||||
{ name: 'ls', age: 38, score: 70 },
|
||||
{ name: 'sb', age: 18, score: 70 }
|
||||
]
|
||||
data4.sort((a, b) => {
|
||||
return a.age - b.age
|
||||
})
|
||||
console.log(data4)
|
||||
|
||||
data4.sort((a, b) => {
|
||||
// 先按分数升序
|
||||
let scoreRst = a.score - b.score
|
||||
// 如果分数相同
|
||||
if (scoreRst == 0)
|
||||
// 按年龄降序
|
||||
return b.age - a.age
|
||||
return scoreRst
|
||||
})
|
||||
console.log(data4)
|
||||
|
||||
/*
|
||||
# node sort.js
|
||||
[ 1, 10, 20, 30, 4 ]
|
||||
[ 1, 4, 10, 20, 30 ]
|
||||
[ 30, 20, 10, 4, 1 ]
|
||||
[
|
||||
{ name: 'sb', age: 18, score: 70 },
|
||||
{ name: 'zs', age: 28, score: 90 },
|
||||
{ name: 'ls', age: 38, score: 70 }
|
||||
]
|
||||
[
|
||||
{ name: 'ls', age: 38, score: 70 },
|
||||
{ name: 'sb', age: 18, score: 70 },
|
||||
{ name: 'zs', age: 28, score: 90 }
|
||||
]
|
||||
*/
|
||||
28
Server/tests/test2/important/splice.js
Normal file
28
Server/tests/test2/important/splice.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// splice(start)
|
||||
// splice(start, deleteCount)
|
||||
// splice(start, deleteCount, item1, ...)
|
||||
|
||||
const datas = ["aa", "bb", "cc"];
|
||||
console.log(datas.splice(1))
|
||||
console.log(datas)
|
||||
|
||||
console.log(datas.splice(1, 1))
|
||||
console.log(datas)
|
||||
|
||||
console.log(datas.splice(1, 0, "xx", "yy"))
|
||||
console.log(datas)
|
||||
|
||||
console.log(datas.splice(1, 1, "xx"))
|
||||
console.log(datas)
|
||||
|
||||
/*
|
||||
# node splice.js
|
||||
[ 'bb', 'cc' ]
|
||||
[ 'aa' ]
|
||||
[]
|
||||
[ 'aa' ]
|
||||
[]
|
||||
[ 'aa', 'xx', 'yy' ]
|
||||
[ 'xx' ]
|
||||
[ 'aa', 'xx', 'yy' ]
|
||||
*/
|
||||
12
Server/tests/test2/important/unshift.js
Normal file
12
Server/tests/test2/important/unshift.js
Normal file
@@ -0,0 +1,12 @@
|
||||
// let len = array.unshift(element1, ..., elementN);
|
||||
|
||||
const animals = ["pigs"]
|
||||
let rst = animals.unshift("dogs", "cats")
|
||||
console.log(rst)
|
||||
console.log(animals)
|
||||
|
||||
/*
|
||||
# node unshift.js
|
||||
3
|
||||
[ 'dogs', 'cats', 'pigs' ]
|
||||
*/
|
||||
Reference in New Issue
Block a user