// 数组去重排序(indexOf方法去重,sort方法排序)
var arr = [1,5,8,6,7,5,6,8,9,10];
function code (arr){
let result = []
for( let i = 0; i<arr.length; i++ ){
if(result.indexOf(arr[i]) == -1){
result.push(arr[i]);
}
};
return result.sort((a,b) => {
return a - b
});
}
console.log(code(arr));
// 数组排序方法(冒泡方法)
var arr1 = [50,60,105,98,202,101,33]
for(let i = 0;i<arr1.length;i++){
for(let j = 0; j<arr1.length - i - 1; j++){
// console.log("j",arr1[j])
// console.log("j+1",arr1[j+1])
if(arr1[j+1] < arr1[j]){
var smpt = arr1[j]
arr1[j] = arr1[j+1]
arr1[j+1] = smpt
}
}
}
console.log(arr1)
// 数组去重方法(filter)
let arr2 = [1,1,5,8,6,7,5,6,8,9,10];
var result1 = arr2.filter((item,index,array) => {
return arr2.indexOf(item) == index
})
console.log(result1)
// 数组去重方法(set)
let arr3 = [1,1,5,8,6,7,5,6,8,9,10];
let test1 = new Set(arr3) // Set方法不允许存在重复的值,会自动去掉重复的值(Set方法会把数组转换成对象)
let result2 = Array.from(test1) // 所以要把Set对象再转换成数组
console.log(result2)
// 数字分隔符
const totalnum = 2000_000_000;
console.log(totalnum);
// ?? (?? 在 JS 中专门检查一个值是否为 null 或undefined。)
let a1;
let b1 = 5;
let c1 = a1 ?? b1
console.log(c1)
// 单行判断简写(if语句,单行条件下可以这样简写)
let d1 = 9
if(!a1) a1 = d1
console.log(a1)