JS中String、Number、Object的常用方法

2023-01-13 0 634

// 建立数组的四种形式: 字面上量, String(), new String() 前面三种是基本上正则表达式 前面是提及正则表达式

// 转化成为数组 1String() 2显式切换+”” 3`${}` 4toString()

let num1 = null;

// num1 = String(num1)

// num1 = `${num1}`

// num1 = new String(num1);

// console.log(typeof num1.valueOf(), num1.valueOf());

// num1 = String(num1);

num1 = `${num1}`

console.log(typeof num1);

console.log(num1.length);

// 常见形式

//charAt() 根据索引返回指定字符

let lstr = hello world!;

console.log(lstr.charAt(lstr.length – 1));

//concat() 连接成新数组 (可以多个),不改变原来数组

let lstr1 = myBaby;

let lstr2 = ,haha

let arr2 = [xixi, haha];

let arr3 = { name: zhangsan, age: 12 }

let arr = lstr1.concat(lstr, lstr2, arr2, arr3);

let arr4 = lstr1.concat(lstr, lstr2, …arr2, arr3);

console.log(arr, arr4);

// endsWith() 是否以某个数组结尾 startsWith()是否以某个数组开始

let str3 = this is my baby!;

let v = str3.endsWith(baby!);

let b = str3.startsWith(this);

console.log(v, b, str3.length);

// includes() 数组中是否包含指定数组,有返回true,没有返回false

let str4 = this mood is my;

let t = str4.includes(mood);

let t1 = str4.includes(tt);

let t2 = str4.includes( );

console.log(t, t1, t2);

// indexOf() 检索数组中是否存在某个数组,有返回指定索引,没有返回-1 从前查找

let str5 = Brave new world;

let index1 = str5.indexOf(new);

let index2 = str5.indexOf(haha);

console.log(index1, index2);

// lastIndexOf() 检索数组中是否存在某个数组,有返回指定索引,没有返回-1 从后查找

let str6 = “Blue Whale, Killer Whale”;

let index3 = str6.lastIndexOf(Whale);

let index4 = str6.lastIndexOf(rtrt);

console.log(index3, index4);

// repeat() 返回指定数量的数组

let syt = baby;

let syt2 = syt.repeat(3);

console.log(syt2, syt);

// replace() 数组替换 相同的替换一个

let rtr = [hahazheshiwo];

let rtr1 = rtr.replace([, *);

let rtr2 = rtr1.replace(], *);

console.log(rtr2, rtr);

// replaceAll() 数组替换 相同的全部替换

let rtr3 = [hahazheshiwo];

let rtr4 = rtr3.replaceAll(ha, “&”);

console.log(rtr4, rtr3);

// slice() 截取数组的一部分数组 [start,end)

// 前包后不包,可负数

// 第二位可省略

let wer = Thequickbrown fox jumps over the lazy dog.;

let wer1 = wer.slice(2, 6); //equi

let wer2 = wer.slice(8, -3) //brown fox jumps over the lazy d

let wer3 = wer.slice(-3, -1); //og

let wer4 = wer.slice(-3); //og.

console.log(wer4);

// substring() 截取数组的一部分数组 [start,end) 可以有负数,负数会转化成为0,再对调

let ert = helloworld;

let ert1 = ert.substring(2, 6); //llow

let ert2 = ert.substring(4); //oworld

let ert3 = ert.substring(2, -4); //he 小于0的数切换为0,再对调

console.log(ert3, ert);

// split() 数组转数组

let qwe = The quickbrown fox jumps over the lazy dog.;

let qweArr = qwe.split( );

let qwe2 = The,quickbrown,fox,jumps,;

let qweArr2 = qwe2.split(,); //[The, quickbrown, fox, jumps, ]

console.log(qweArr2);

// trim() 去除数组前后空格 trimEnd()去除后空格 trimStart()去除前空格

let rty = erouy ;

let rty1 = rty.trim();

let rty2 = rty.trimEnd();

let rty3 = rty.trimStart();

console.log(rty3, rty3.length);

// toLowerCase() 数组转小写 toUpperCase()转大写

let uio = ALphAEBT;

let uio1 = uio.toLowerCase(); //alphabet

let uio2 = erTYhi;

let uio3 = uio2.toUpperCase(); //ERTYHI

console.log(uio3);

// const zhiStr = To be, or not to be, that is the question.;

// let count = 0;

// let indexc = zhiStr.indexOf(e);

// while (indexc != -1) {

// count++;

// indexc = zhiStr.indexOf(e, indexc + 1)

// }

// console.log(`找到了${count}次`);

// Number形式

let num2 = 10.2;

//Number.isInteger 判断数字是否为整数

console.log(Number.isInteger(num2));

// Number.isNaN 判断是否为NaN

console.log(Number.isNaN(NaN));

let str7 = 348hakj;

// 数组转数字

let num3 = Number.parseInt(str7); //348

console.log(num3);

// 数组转浮点数

let str8 = 348.999uhakj;

let num4 = Number.parseFloat(str8); //348.98

console.log(num4);

// toFixed() 四舍五入指定的小数点位

let str9 = num4.toFixed(2); //348.99

console.log(str9);

// Object形式

// Object.is() 比较两个值是否相等 和 ===一致

console.log(Object.is(12, 12));

let abj = { name: zhangsan, age: 12 };

let abj2 = { name: zhangsan, age: 12 };

console.log(Object.is(abj, abj2));

// Object.assign() 合并拷贝对象 如果有相同属性的前面将前面的覆盖

// 对象中属性为基本上类型,就是深拷贝

// 对象中属性为提及类型,就是浅拷贝

let bbr = { a: 1, b: 2 };

let bbr2 = { b: 3, c: 5 };

Object.assign(bbr, bbr2);

let bbr3 = Object.assign({}, bbr2)

bbr2.b = haha;

console.log(bbr3, bbr2); //{b: 3, c: 5} {b: haha, c: 5}

let waiObj = { name: lisi, type: { age: 12, sex: 男 } };

let waiObj1 = Object.assign({}, waiObj);

waiObj1.type.sex = 女;

console.log(waiObj, waiObj1); // 两个值都是 name :”lisi”,type: {age: 12, sex: 女}

// Object.values() 取对象的属性值构成的数组 Object.keys()取对象的属性构成的数组

let trr = [er, haha, rtrt];

let trr1 = Object.values(trr);

let trr2 = { name: zhansan, type: { age: 12, sex: 男 } };

let trr3 = Object.values(trr2); //[“zhansan”,{age: 12, sex: 男}]

let trr4 = Object.keys(trr2);

console.log(trr4);

// Object.entries() 键值对数组

let trr5 = { a: somestring, b: 42 };

let trr6 = Object.entries(trr5); //[[a, somestring], [b, 42]]

console.log(trr6);

JS中String、Number、Object的常用方法
举报/反馈

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务