<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery-3.3.1.min.js"></script>
</head>
<body>
<div id="result"></div>
</body>
<script>
$(function(){
var data = "q,w,e,r,d,f,a,1,2,3";
var dataArr = data.split(",");
var dataArr_Ascii = dataArr.concat();//复制数组
console.log(dataArr);
var idx = [];
//也可以用正则表达式,这里用的是ASCII码
var newArr = $.each(dataArr_Ascii,function(key,val){
dataArr_Ascii[key] = dataArr_Ascii[key].charCodeAt();
});
console.log(newArr,dataArr_Ascii,dataArr);
//48~57为0到9十个阿拉伯数字
//注意 仅适用于0~9的情况
//多位数字就得用正则把数字取出来了
$("#result").append("数据中是数字的有:");
$.grep(newArr,function(val,key){
if(val < 57 && val > 48 ){
var fCCVal = String.fromCharCode(val);
$("#result").append(fCCVal + ",");
}
// console.log(String.fromCharCode(val));
});
$("#result").append("前五位:");
var front5 = $.grep(dataArr,function(val,key){
return key > 4;
},true);//表示反向结果,下标的值小于5
$.each(front5,function(key,val){
$("#result").append(val + ",");
});
$.grep(dataArr_Ascii,function(val,key){
console.log(dataArr_Ascii);
if(val < 57 && val > 48 ){
idx.push(String.fromCharCode(val));
}
});
console.log(idx);
$.each(idx,function(key,val){
dataArr_Ascii.splice($.inArray(val,dataArr_Ascii),1);//$.inArray()获取数组值的下标,再用splice()将其从数组中删除
});
console.log(dataArr_Ascii);
})
</script>
</html>