<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<img src="img/video_image02.png" >
</body>
<script type="text/javascript">
(function setFun (){
const arr =[1,1,"1","1",2,2];
console.log(new Set(arr)); // 返回值为一个对象: {1, "1", 2}
});
(function formFun(){
const arr =[1,1,"1","1",2,2];
console.log(Array.from(new Set(arr))); // 返回值为一个数组: [1, "1", 2]
// arr = Array.from(new Set(arr))
// console.log(arr)
})()
function sortFun(){
var arr = ['100', '10', '1000'];
console.log(arr.sort()); //不穿参数时,默认的是升序排列 ['10','100','1000']
console.log(arr.sort(function(a,b){
return a - b;
})); //第一个值大于第二个是升序 ['10','100','1000']
console.log(arr.sort(function(a,b){
return b - a;
})); //第二个值大于第一个是升序 ['1000','100','10']
}
sortFun();
function splicefun(){
var arr = ['100', '10', '1000'];
arr1 = arr.splice(1,1,"30");
console.log(arr1); // ['10']
console.log(arr); // ['100', '30', '1000']
};
function splicefun(){
// 数组的遍历
var a = [5,6,7,8];
console.log(a.splice(1,0,9)); //[]
console.log(a); // [5, 9, 6, 7, 8]
var b = [5,6,7,8];
console.log(b.splice(1,2,3)); //[6, 7]
console.log(b); //[5, 3, 8]
}
splicefun();
// 数组的检测
const array = [1,1,2,3];
console.log(array instanceof Array);//true
console.log(array.constructor === Array); //true
console.log(typeof(array)); //object
console.log(Array.isArray(array)) // true
// const array = new Array();
// array.constructor === Array; // true
// const array = new Array();
Array.isArray(array); //true
// let time = DateTime.local()
// .setZone('America/New_York')
// .toFormat('yyyy-MM-dd HH:mm:ss');
// console.log(time);
function sort(arr) {
if (arr.length===1) return arr
//分成两部分
let mid=Math.floor(arr.length/2)
let [part1,part2]=[sort(arr.slice(0,mid)),sort(arr.slice(mid))]
//对比+合并
let result=[]
while (part1.length>0&&part2.length>0)
result.push((part1[0]<part2[0]?part1:part2).shift())
return [...result,...part1,...part2]
}
var need =[10,15,30,16,18,3,50,100,2];
console.log(sort(need));
// var part1 = [1,3,5];
// var part2 = [2,8,7];
// console.log(part1[0]<part2[0]?part1:part2);
// console.log((part1[0]<part2[0]?part1:part2).shift());
// console.log(part1)
// console.log(part2)
</script>
</html>
es6中一些新增的js方法(常用的函数汇总)
最新推荐文章于 2024-10-08 12:51:09 发布