数组技巧练习
var fruits1=["apple","banana"];
var fruits2=["apple2","banana2"];
var fruits3=[...fruits1,...fruits2];
var fruits4=["orange","apple","apple"];
var colors = ["blue","black","white"];
console.log("在数组中随机抽取五个");
for(var i=0;i<5;i++){
//在数组中随机抽取
var randomColor = colors[(Math.floor(Math.random() * (colors.length)))]
console.log(randomColor);
}
//求4中不重复的元素
var fruits5=[...new Set(fruits4)];
//求1和4的交集
var fruits6=[...new Set(fruits4)].filter(item=>fruits1.includes(item))
console.log({...fruits4});
console.log(fruits3);
console.log("求4中不重复的元素:"+fruits5)
console.log("求1和4的交集:"+fruits6);
js实现进制转换
使用toString()即可实现
例:
var x=16;
var y=x.toString(16);
console.log(y);
js实现补全字符串
使用String.padStart(4,0)和String.y.padEnd(4,0)补全字符串
其中第一个参数为补全位数,第二个为补全内容
例子:
var x='10'
x.padStart(4,0)
console.log(x)
//输出 0010