A. js ajax獲取的數據,怎麼變成數組格式
varnameList=[];
for(vari=0;i<data.a.length;i++){
nameList[nameList.length]=data.a[i];
}
//nameList就是抄你襲想要的東西
B. js如何將一個數組對象轉換成另外一種格式的數組對象
vararr=["中國|廣東|深圳","中國|廣東|深圳","中國|湖北|黃石","中國|湖北|武漢","中國|廣東|廣州","中國|廣東|廣州","中國|湖北|武漢","中國|福建|福州","中國|福建|廈門"];
varresult={};
vartemp={};
for(vari=0;i<arr.length;i++){
varitem=arr[i];
varits=item.split('|');//按|拆分
varcountry=its[0];//國家
varprovince=its[1];//省
varcity=its[2];//城市
if(!result[country]){//國家不在結果對象里
result[country]=[];//添加
}
if(!temp[province]){//臨時對象沒有省信息
result[country].push(temp[province]={//記入結果數組
province:[province],
city:[],
count:[]
});
}
varinfo=temp[province];
varfound=false;
for(varj=info.city.length-1;j>=0;j--){//在省信息的城市數組里查找是否已經存在
if(info.city[j]==city){
found=true;//已經存在
info.count[j]=info.count[j]+1;//更新count
}
}
if(!found){//未找到城市
info.city.push(city);//添加
info.count.push(1);//count初始值為1
}
}
console.log(result);
C. js中有一個字元串,字元串的格式與js數組定義的格式一樣,怎樣轉換成數組
var str = "[{name:'aaaa',value:'1111'},{name:'aaaa',value:'1111'}]";
var array = eval ("(" + str + ")");
console.log(array);
D. JS中如何把數組轉化成JSON格式
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setExcludes(excludes);
JSONArray fromObject = JSONArray.fromObject(obj,jsonConfig);
String json = fromObject.toString();//json格式的字元串
E. js,如何把數組格式的String轉化為數組
var s = "111,222,333,444";
var arr = s.split(",");
//arr即是轉化後的數組
F. js怎麼輸出數組
可以用alert函數看裡面的值的
但是必須用for循環
var array = ["1","2","3","4"]; //your array here
for(count=0;count<array.length;count++){
alert(array[count]);
}
或
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>menu</title>
<style type="text/css">
</style>
<script type="text/javascript">
console.log(target)
</script>
</head>
<body>
</body>
</html>
(6)jsarray格式擴展閱讀:
注意:無回參數的函數必須在答其函數名後加括弧:
function函數名 () {代碼... }
function函數名 () {代碼... }
注意:別忘記 JavaScript 中大小寫字母的重要性。"function" 這個詞必須是小寫的,否則 JavaScript 就會出錯。另外需要注意的是,必須使用大小寫完全相同的函數名來調用函數。
G. javascript中Array的類型為什麼是object
instanceof檢測實例是否有繼承某類。 console.log(new Array() instanceof Array); console.log([] instanceof Array); 而 typeof 檢測出來不精準很多,js里的對象都是object。(%#@¥回#……%¥@&@¥%&¥…答…) 再摘一段犀牛書中的描述: 5.10.2 typeof 運算符 如果 typeof 的運算數是數字、字元串或者布爾值,它返回的結果就是 「number」、「string」或「boolean」。對對象、數組和null,它返回的是「object」。對函數運算數,它返回的是「function」。
H. 在JavaScript中建立數組對象時的三個格式最正規的應該怎麼寫
var myArray = new Array(3);
// 添加抄數襲據
myArray[0] = "Hello";
myArray[1] = 42;
myArray[2] = new Date(2000, 1, 1);
var myArray = new Array(3);
// 添加數據
myArray[0] = "Hello";
myArray[1] = 42;
myArray[2] = new Date(2000, 1, 1);
你也可以這樣寫
I. JS中,數組中日期格式轉換的問題。。
vararr=[關鍵是源使用js的replace函數,裡面第一個參數是正則表達式,第二個參數是替換後的值
{"riqi":"2017-10-19","xinpai_db":"8","xinpai_gz":"8"},
{"riqi":"2017-10-20","xinpai_db":"3","xinpai_gz":"0"},
{"riqi":"2017-10-21","xinpai_db":"0","xinpai_gz":"0"}
];
for(variinarr){
arr[i].riqi=arr[i].riqi.replace(/-/g,'/');
}
console.log(arr);
J. js控制數組的所有樣式
准備事件沒有放在ready事件裡面
<html>
<head>
<title>style改變樣式</title>
</head>
<script src="js/jquery-1.10.1.min.js" type="text/javascript"></script>
<style type="text/css">
li{
font-size:12px;
color:#000000;
background:#f7f7f7;
width:104px;
height:33px;
line-height:38px;
float:left;
list-style:none;
text-align:center;
margin-left:2px;
}
</style>
<script type="text/JavaScript">
// document.getElementsByTagName("li"); 獲取所有的li標簽
$(document).ready(function(){
var len = document.getElementsByTagName("li");
alert(len);
//為每一個li設置事件和效果
for(var i=0;i < len.length;i++){
len [i].onmouseover = function(){
this.style.background="#777777";
}
len [i].onmouseout = function(){
this.style.background="#f7f7f7";
}
}
});
</script>
<body>
<ul>
<li>新聞</li>
<li>視頻</li>
<li>小說</li>
</ul>
</body>
</html>