目录
js对不同数据类型的操作和具体处理方法的示例
JavaScript中有七种不同的数据类型:
- Number(数字)
- String(字符串)
- Boolean(布尔值)
- Symbol(符号)
- Object(对象)
- Function(函数)
- Undefined(未定义)
下面是一些常见操作和具体处理方法的示例:
-
Number:
- 加法:1 + 2 = 3
- 减法:5 - 2 = 3
- 乘法:3 * 4 = 12
- 除法:10 / 5 = 2
- 取余:10 % 3 = 1
-
String:
- 连接:"Hello, " + "world!" = "Hello, world!"
- 长度:"Hello, world!".length = 13
- 索引:"Hello, world!"[0] = "H"
- 切片:"Hello, world!".slice(7, 12) = "world"
-
Boolean:
- 与:true && false = false
- 或:true || false = true
- 非:!true = false
-
Symbol:
- 创建:let sym = Symbol("foo")
- 描述:sym.description = "foo"
-
Object:
- 属性:let obj = {name: "John", age: 30}; obj.name = "John"
- 键值对:obj["age"] = 30
- 方法:obj.greet = function() {console.log("Hello, my name is " + this.name);}; obj.greet() // "Hello, my name is John"
-
Function:
- 定义:function add(a, b) {return a + b;}
- 调用:add(1, 2) = 3
-
Undefined:
- 变量未赋值时:let x; console.log(x) // undefined
- 函数未返回值时:function doNothing() {} console.log(doNothing()) // undefined
注意:JavaScript中还有一个特殊的数据类型null,它表示一个空对象指针,可以用来明确地表示某些变量没有有效值。
- Null:
- 变量赋值为空对象指针:let x = null; console.log(x) // null
不同数据类型的操作和具体处理方法:
1、Number
let a = 1, b = 2;
console.log(a + b); // 3
console.log(a - b); // -1
console.log(a * b); // 2
console.log(a / b); // 0.5
console.log(a % b); // 1
console.log(Math.round(a / b)); // 1
2、String
let str1 = 'Hello, ';
let str2 = 'world!';
console.log(str1 + str2); // "Hello, world!"
console.log(str1.concat(str2)); // "Hello, world!"
console.log(str1.length); // 7
console.log(str1[0]); // "H"
console.log(str1.slice(2, 5)); // "llo"
3、Boolean
let isTrue = true;
let isFalse = false;
console.log(isTrue && isFalse); // false
console.log(isTrue || isFalse); // true
console.log(!isTrue); // false
4、Symbo
let sym1 = Symbol('foo');
let sym2 = Symbol('foo');
console.log(sym1 === sym2); // false
console.log(sym1.toString()); // "Symbol(foo)"
console.log(sym1.description); // "foo"
5、Object
let obj = {name: "John", age: 30};
console.log(obj.name); // "John"
console.log(obj["age"]); // 30
obj.greet = function() {console.log("Hello, my name is " + this.name);};
obj.greet(); // "Hello, my name is John"
6、Function
function add(a, b) {
return a + b;
}
console.log(add(1, 2)); // 3
7、Undefined
let x;
console.log(x); // undefined
js中用函数处理各类复杂数据结构的示例
定义函数的基本语法如下:
function functionName(parameters) {
// code to be executed
return result;
}
例如,下面是一个简单的函数,用于计算两个数字的和:
function add(a, b) {
return a + b;
}
调用函数的基本语法如下:
functionName(arguments);
例如,调用上面的 add 函数:
add(1, 2); // returns 3
使用函数可以处理各种复杂的数据结构。例如:
- 数组: 您可以定义一个函数来处理数组中的元素,例如过滤,映射,排序等
function filterArray(arr, callback) { let newArr = []; for(let i = 0; i < arr.length; i++) { if(callback(arr[i])) { newArr.push(arr[i]); } } return newArr; } let numbers = [1, 2, 3, 4, 5]; let evenNumbers = filterArray(numbers, function(number) { return number % 2 === 0; }); console.log(evenNumbers); // [2, 4]
- 对象: 您可以使用函数来遍历对象的属性,并执行特定的操作,例如计算总和或查找最大值
function sumProperties(obj) {
let sum = 0;
for(let property in obj) {
if(obj.hasOwnProperty(property)) {
sum += obj[property];
}
}
return sum;
}
let object = { a: 1, b: 2, c: 3 };
console.log(sumProperties(object)); // 6
总之,使用函数可以使代码更具可重用性和可维护性。可以使用函数来封装常用的操作,并在需要时重复使用。函数还可以接受参数,并返回结果,这样就可以在不同的场景中使用相同的代码。这对于处理复杂的数据结构是非常有用的,可以使用函数来组织和操作数据,并确保代码的可读性和可维护性。
js实现排序算法、搜索算法、数据结构、加密算法代码示例
1、排序算法 - 冒泡排序:
// 定义冒泡排序函数
function bubbleSort(arr) {
// 遍历整个数组
for (let i = 0; i < arr.length; i++) {
// 比较相邻元素并交换位置
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
let temp = arr[j];
arr[j] = arr
2、搜索算法 - 二分查找:
// 定义二分查找函数
function binarySearch(arr, target) {
// 设置初始查找范围
let left = 0;
let right = arr.length - 1;
// 循环查找
while (left <= right) {
// 计算中间索引
let mid = Math.floor((left + right) / 2);
// 判断中间值是否等于目标值
if (arr[mid] === target) {
return mid;
}
// 如果中间值小于目标值,则在右半部分继续查找
else if (arr[mid] < target) {
left = mid + 1;
}
// 如果中间值大于目标值,则在左半部分继续查找
else {
right = mid - 1;
}
}
// 如果未找到目标值,则返回-1
return -1;
}
// 使用示例
let numbers = [1, 2, 3, 4, 5];
console.log(binarySearch(numbers, 3)); // 2
3、数据结构 - 链表:
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
// 在链表末尾添加元素
append(value) {
let node = new Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
this.length++;
}
// 在链表开头添加元素
prepend(value) {
let node = new Node(value);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head = node;
}
this.length++;
}
// 查找链表中指定索引的元素
get(index) {
if (index < 0 || index >= this.length) {
return null;
}
let current = this.head;
for (let i = 0; i < index; i++) {
current = current.next;
}
return current;
}
// 删除链表中指定索引的元素
remove(index) {
if (index < 0 || index >= this.length) {
return null;
}
if (index === 0) {
this.head = this.head.next;
this.length--;
return;
}
let previousNode = this.get(index - 1);
previousNode.next = previousNode.next.next;
this.length--;
}
}
// 使用示例
let list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
console.log(list.get(1).value); // 2
list.remove(1);
console.log(list.get(1).value); // 3
4、加密算法 - RSA
const crypto = require('crypto');
// 生成随机秘钥
let { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',