diff --git a/Server/tests/test2/important/every.js b/Server/tests/test2/important/every.js new file mode 100644 index 0000000..a929ccc --- /dev/null +++ b/Server/tests/test2/important/every.js @@ -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 +*/ \ No newline at end of file diff --git a/Server/tests/test2/important/filter.js b/Server/tests/test2/important/filter.js new file mode 100644 index 0000000..1fd7f0d --- /dev/null +++ b/Server/tests/test2/important/filter.js @@ -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 } +] +*/ diff --git a/Server/tests/test2/important/find.js b/Server/tests/test2/important/find.js new file mode 100644 index 0000000..eba3ffe --- /dev/null +++ b/Server/tests/test2/important/find.js @@ -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 } +] +*/ \ No newline at end of file diff --git a/Server/tests/test2/important/findIndex.js b/Server/tests/test2/important/findIndex.js new file mode 100644 index 0000000..197f894 --- /dev/null +++ b/Server/tests/test2/important/findIndex.js @@ -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 +*/ \ No newline at end of file diff --git a/Server/tests/test2/important/reduce.js b/Server/tests/test2/important/reduce.js new file mode 100644 index 0000000..5d43c67 --- /dev/null +++ b/Server/tests/test2/important/reduce.js @@ -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 } ] +} +*/ diff --git a/Server/tests/test2/important/slice.js b/Server/tests/test2/important/slice.js new file mode 100644 index 0000000..e4be908 --- /dev/null +++ b/Server/tests/test2/important/slice.js @@ -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' ] +*/ diff --git a/Server/tests/test2/important/some.js b/Server/tests/test2/important/some.js new file mode 100644 index 0000000..c7095c6 --- /dev/null +++ b/Server/tests/test2/important/some.js @@ -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 +*/ \ No newline at end of file diff --git a/Server/tests/test2/important/sort.js b/Server/tests/test2/important/sort.js new file mode 100644 index 0000000..6743017 --- /dev/null +++ b/Server/tests/test2/important/sort.js @@ -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 } +] +*/ diff --git a/Server/tests/test2/important/splice.js b/Server/tests/test2/important/splice.js new file mode 100644 index 0000000..f28cbc1 --- /dev/null +++ b/Server/tests/test2/important/splice.js @@ -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' ] +*/ diff --git a/Server/tests/test2/important/unshift.js b/Server/tests/test2/important/unshift.js new file mode 100644 index 0000000..74dc030 --- /dev/null +++ b/Server/tests/test2/important/unshift.js @@ -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' ] +*/ \ No newline at end of file