JavaScript基础知识:数据类型

一、舍入

Math.floor 向下舍入 3.1 --> 3 , -1.1 --> -2
Math.ceil 向上舍入 3.1 --> 4, -1.1 --> -1
Math.round 向附近取整 3.1 —> 3, 3.4 --> 4, -1.1 --> 1

如果想要把小数舍入到指定的位数时,有两种方法:
1)使用 Math.floor

let num = 1.23456;
alert(Math.floor(num*100)/100); // 1.23456 -> 123.456 -> 123 -> 1.23

2)使用toFixed(n) ( n指定舍入后小数点后几位 )

let num = 1.23456
alert(num.toFixed(2));

/================================================================================/

Math.roundtoFixed(n) 都将数字舍入到最接近的数字:0…4 会被舍去,而 5…9 会进一位。
但是使用toFixed时,会遇到这种问题: 6.35.toFixed(1) == 6.3 而不是我们所理解的 6.35.toFixed(1) == 6.4
这就涉及到很多语言都会有的问题:精度丢失。举个最基本的例子:

alert( 0.1 + 0.2 == 0.3 ); // false

原因:数字是以 64 位格式 IEEE-754 表示的,所以正好有 64 位可以存储一个数字:其中 52 位被用于存储这些数字,其中 11 位用于存储小数点的位置(对于整数,它们为零),而 1 位用于符号。

6.35.toFixed(20); //   alert 结果为 6.34999999999999964473

解决:
1)在舍入前应该使其更接近于整数,所以:

alert((6.35*10).toFixed(20));  //alert结果: 63.50000000000000000000

2)然后再使用就近取整对其舍入:

alert(Math.round(6.35*10)/10);  //alert结果: 6.4

/================================================================================/

isFinite(value) 将其参数转换为数字,如果是常规数字,则返回 true,而不是 NaN/Infinity/-Infinity

有时 isFinite 被用于验证字符串值是否为常规数字

常用例子:
1)取整
丢掉小数,只保留整数部分

parseInt(5/2); // 2

2)向上取整
有小数,整数部分加1

Math.ceil(5/2); // 3

3)向下取整
有小数,直接丢弃小数,整数部分不变

Math.floor(5/2); // 2

4)四舍五入
小数部分(0-4)舍掉,(5-9)整数部分加1

Math.round(5/2); // 3

二、字符串

1.获取字符串长度 : length

2.访问字符

想要获取某个字符串中的某一个字符,可以这样:

let str = "hellow";

//获取第一个字符; 下面第一种是现代化方法
//两者的区别是当没有找到字符时,[]返回的是undefined,chartAt返回的是空字符串''
alert(str[0]); //h  
alert(str.chartAt(0));  //h

//获取最后一个字符
alert(str[str.length-1]) // o

以上两种方法只可以访问字符,而不能改变字符串,如果想改变字符串,只能创建新的字符串,例如:

let str = 'Hello';
str = 'h' + str.slice(1,5);
alert(str); //hello

3.获取字符串

javascript中有三种方法: substring, substr, slice

在此只记录下常用的slice:
str.slice(start[, end])
1)返回的是字符串从start位置到end(但不包括end=)的字符串
2)如果没有end参数,slice会一直运行到字符串末尾

三、数组

1.清空数组

1)关于清空数组:arr=[]; 与**arr.length = 0;**的区别

    let small = [1,2,3,];
	let big = [4,5,6];
	let small2 = small;
	let big2 = big;
	console.log('small',small);
	console.log('big',big);
	console.log('small2',small2);
	console.log('big2',big2);
	small = [];
	big.length = 0; 
	
	console.log('***清空后**')
	console.log('small',small);
	console.log('big',big);
	console.log('small2',small2);
	console.log('big2',big2);

运行结果:
在这里插入图片描述
知识点:数组是一种特殊的对象,是通过引用来复制

let small2 = small; 属于浅拷贝;要想进行数组的深拷贝,应该对数组进行遍历(for)赋值(对象的深拷贝有两种方式遍历及jq的$.extend);

2.数组方法

1)添加/移除数组方法
常见的几种方法如下,在此就不做详细解释啦:

push()-------------- 尾端添加
pop()---------------- 尾端移除
unshift()----------- 首端添加
shift()--------------- 首端移除

还有其它常见的方法,在此详细解释下:

splice可以做到添加、删除、插入元素
a)语法:arr.splice(start[, deleteCount, elem1, …, elemN])
b)说明:
start ------------------------- 从start处开始修改
deletCount ---------------- 删除元素的个数
elem1, … ------------------ 从start处开始要插入的元素
c)返回值:返回已删除(操作后)的数组

let arr=["apple","banana","grape"];
arr.splice(1,1,"orange","watermelon");
console.log(arr);   //["apple","orange","watermelon","grape"]

slice
a)语法:arr.slice([start],[end])
b)说明:将所有从索引 start 到 end(不包括 end)的数组项复制到一个新的数组
c)返回值:返回新数组
d)可不带参数使用:通常用于获取副本,来进一步转换而不影响原数组,例子如下:

let arr=["apple","banana","grape"];
let arr2 = arr.slice();
arr2.splice(1,1);
console.log('arr:',arr);
console.log('arr2',arr2);

在这里插入图片描述

concat
1)语法:arr.concat(arg1, arg2…)
2)说明:创建一个新数组,其中包含来自于其他数组和其他项的值。(通常用于连接两个数组)

let arr=["apple","banana","grape"];
let arr2=["orange","watermelon"];
console.log(arr.concat(arr2));  // ["apple", "banana", "grape", "orange", "watermelon"]

2)转化数组方法
map、sort、reverse、split/join、reduce/reduceRight

map()
1)作用:返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值

let arr = [4,9,16,25,36];
function test(){
	//将数组中的数开平方
	arr.map(Math.sqrt);
	console.log('开平方后', arr.map(Math.sqrt));
	console.log('原数组不变', arr);
	
}
test();

在这里插入图片描述

sort()
1)语法:sort(sortfunction)
2)说明:
sortfunction:可选,规定排序顺序,必须是函数;
默认升序排列;
3)返回值:原数组

reverse()
1)作用:用于颠倒数组中的元素
2)返回值:颠倒后的数组(不是新数组

split()
1)作用:字符串 转 数组
2)语法:string.split(separator,limit)
separator:可选,字符串或正则表达式,如果是"",则字符串的每个字符都会被分割;
limit: 可选,可指定返回的数组的最大长度(一般不用)
3)返回值:返回数组,不改变原有字符串

//相同字符串,不同的分隔字符串得到的结果
let str="apple,banana,grape";
let arr = str.split("a");
let arr2 = str.split(",");
console.log('以a分隔',arr);
console.log("以逗号分隔",arr2);

在这里插入图片描述

join()
1)作用:与 split() 相反,数组 转 字符串
2)语法:arr.join(separator)
separator:可选,指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。
3)返回值:字符串

let arr=["apple","banana","grape"];
let str = arr.join("---");
console.log(str);  //apple---banana---grape

reduce()

当我们遍历数组的时候,可以用forEach,for,for…of
遍历数组并返回值时,可以用map
当我们相对数组的每一项求和时,可以用reduce

1)语法:
let value = arr.reduce(function(accumulator, item, index, array) {
// …
}, [initial]);

2)说明:
accumulator:上一次调用的结果,第一次==initial(初始值);
item: 当前数组元素;
index: 当前索引;
array: 当前数组;

应用函数时,上一个函数调用的结果将作为第一个参数传递给下一个函数;
所以第一个参数相当于累加器,用来存储之前的结果,最后成为 reduce 的结果;

let arr = [1, 2, 3, 4, 5];
let result = arr.reduce((sum, current) => sum + current, 0);
alert(result); // 15

以上内容参考于https://zh.javascript.info/object-methods,在此学习记录。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值