@lppx/nlearn 1.1.14 → 1.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/demo/data-types/02-/346/225/260/347/273/204/344/270/216/345/205/203/347/273/204.js
CHANGED
|
@@ -57,9 +57,22 @@ function demonstrateArrayOperations() {
|
|
|
57
57
|
// map 转换
|
|
58
58
|
const upperFruits = fruits.map(fruit => fruit.toUpperCase());
|
|
59
59
|
console.log('\nmap 转大写:', upperFruits);
|
|
60
|
+
// reduce 累积计算
|
|
61
|
+
const numbers = [1, 2, 3, 4, 5];
|
|
62
|
+
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
|
|
63
|
+
console.log('\nreduce 求和:', sum);
|
|
64
|
+
// reduce 统计字符串长度总和
|
|
65
|
+
const totalLength = fruits.reduce((total, fruit) => total + fruit.length, 0);
|
|
66
|
+
console.log('reduce 总长度:', totalLength);
|
|
67
|
+
// reduce 构建对象
|
|
68
|
+
const fruitLengths = fruits.reduce((obj, fruit) => {
|
|
69
|
+
obj[fruit] = fruit.length;
|
|
70
|
+
return obj;
|
|
71
|
+
}, {});
|
|
72
|
+
console.log('reduce 构建对象:', fruitLengths);
|
|
60
73
|
// filter 过滤
|
|
61
74
|
const filtered = fruits.filter(fruit => fruit.length > 5);
|
|
62
|
-
console.log('
|
|
75
|
+
console.log('\nfilter 长度>5:', filtered);
|
|
63
76
|
}
|
|
64
77
|
// #endregion
|
|
65
78
|
// #region 示例3: 只读数组
|
|
@@ -11,6 +11,7 @@ exports.assignmentOperators = assignmentOperators;
|
|
|
11
11
|
exports.comparisonOperators = comparisonOperators;
|
|
12
12
|
exports.logicalOperators = logicalOperators;
|
|
13
13
|
exports.stringOperators = stringOperators;
|
|
14
|
+
exports.spreadOperator = spreadOperator;
|
|
14
15
|
// #region 示例1: 算术运算符
|
|
15
16
|
function arithmeticOperators() {
|
|
16
17
|
console.log('=== 算术运算符 ===');
|
|
@@ -119,13 +120,72 @@ function stringOperators() {
|
|
|
119
120
|
console.log(`字符串 - 数字: '${str}' - ${num} = ${str - num}`); // 数字运算(自动转换)
|
|
120
121
|
}
|
|
121
122
|
// #endregion
|
|
123
|
+
// #region 示例6: 展开运算符 (...)
|
|
124
|
+
function spreadOperator() {
|
|
125
|
+
console.log('\n=== 展开运算符 (...) ===');
|
|
126
|
+
// 数组展开
|
|
127
|
+
const arr1 = [1, 2, 3];
|
|
128
|
+
const arr2 = [4, 5, 6];
|
|
129
|
+
const combined = [...arr1, ...arr2];
|
|
130
|
+
console.log(`合并数组: [${combined}]`);
|
|
131
|
+
// 数组复制(浅拷贝)
|
|
132
|
+
const original = [1, 2, 3];
|
|
133
|
+
const copy = [...original];
|
|
134
|
+
copy.push(4);
|
|
135
|
+
console.log(`\n原数组: [${original}]`);
|
|
136
|
+
console.log(`复制数组: [${copy}]`);
|
|
137
|
+
// 在数组中插入元素
|
|
138
|
+
const numbers = [1, 2, 5, 6];
|
|
139
|
+
const inserted = [1, 2, ...[3, 4], 5, 6];
|
|
140
|
+
console.log(`\n插入元素: [${inserted}]`);
|
|
141
|
+
// 对象展开
|
|
142
|
+
const person = { name: '张三', age: 25 };
|
|
143
|
+
const employee = { ...person, job: '工程师', salary: 10000 };
|
|
144
|
+
console.log(`\n员工信息:`, employee);
|
|
145
|
+
// 对象合并(后面的属性会覆盖前面的)
|
|
146
|
+
const defaults = { theme: 'light', fontSize: 14, showSidebar: true };
|
|
147
|
+
const userSettings = { fontSize: 16, showSidebar: false };
|
|
148
|
+
const finalSettings = { ...defaults, ...userSettings };
|
|
149
|
+
console.log(`\n最终设置:`, finalSettings);
|
|
150
|
+
// 对象复制(浅拷贝)
|
|
151
|
+
const originalObj = { x: 1, y: 2 };
|
|
152
|
+
const copiedObj = { ...originalObj };
|
|
153
|
+
copiedObj.x = 10;
|
|
154
|
+
console.log(`\n原对象:`, originalObj);
|
|
155
|
+
console.log(`复制对象:`, copiedObj);
|
|
156
|
+
// 函数参数展开
|
|
157
|
+
function sum(a, b, c) {
|
|
158
|
+
return a + b + c;
|
|
159
|
+
}
|
|
160
|
+
const nums = [1, 2, 3];
|
|
161
|
+
const result = sum(...nums);
|
|
162
|
+
console.log(`\nsum(...[1, 2, 3]) = ${result}`);
|
|
163
|
+
// Math 函数中使用
|
|
164
|
+
const values = [5, 2, 8, 1, 9, 3];
|
|
165
|
+
const max = Math.max(...values);
|
|
166
|
+
const min = Math.min(...values);
|
|
167
|
+
console.log(`\n数组 [${values}]`);
|
|
168
|
+
console.log(`最大值: ${max}`);
|
|
169
|
+
console.log(`最小值: ${min}`);
|
|
170
|
+
// 字符串展开为字符数组
|
|
171
|
+
const str = 'Hello';
|
|
172
|
+
const chars = [...str];
|
|
173
|
+
console.log(`\n字符串展开: '${str}' => [${chars.map(c => `'${c}'`).join(', ')}]`);
|
|
174
|
+
// 去重(结合 Set)
|
|
175
|
+
const duplicates = [1, 2, 2, 3, 3, 3, 4, 5, 5];
|
|
176
|
+
const unique = [...new Set(duplicates)];
|
|
177
|
+
console.log(`\n去重前: [${duplicates}]`);
|
|
178
|
+
console.log(`去重后: [${unique}]`);
|
|
179
|
+
}
|
|
180
|
+
// #endregion
|
|
122
181
|
if (require.main === module) {
|
|
123
182
|
const examples = [
|
|
124
183
|
arithmeticOperators,
|
|
125
184
|
assignmentOperators,
|
|
126
185
|
comparisonOperators,
|
|
127
186
|
logicalOperators,
|
|
128
|
-
stringOperators
|
|
187
|
+
stringOperators,
|
|
188
|
+
spreadOperator
|
|
129
189
|
];
|
|
130
190
|
const exampleNumber = parseInt(process.argv[2]);
|
|
131
191
|
if (exampleNumber && exampleNumber > 0 && exampleNumber <= examples.length) {
|
package/package.json
CHANGED