数组创建
// 常规方式
var myCars=new Array();
// 简介方式
var myCars=new Array("Saab","Volvo","BMW");
// 字面方式
var myCars=["Saab","Volvo","BMW"];
数组数据获取
使用索引获取值
var myCars = ["Saab", "Volvo", "BMW"];
console.log(myCars);
console.log(myCars[1]);
数组遍历
for循环遍历
<script>
var myCars = ["Saab", "Volvo", "BMW"];
for (var i =0;i<myCars.length;i++){
console.log(myCars[i]);
}
</script>
while循环遍历
var i =0;
while(i<myCars.length){
console.log(myCars[i]);
i++;
}
<script>
window.onload = function() {
var aData = [{
"spid": "01",
"imgUrl": "./imges/1c.webp",
"proName": " 小米11 Ultra ",
"proPrice": "5499",
"proComm": "6.6"
}, {
"spid": "02",
"imgUrl": "./imges/2c.webp",
"proName": " 米家台灯1S ",
"proPrice": "179",
"proComm": "12.2"
}, {
"spid": "03",
"imgUrl": "./imges/3c.webp",
"proName": " 米家智能IH电饭煲 3L ",
"proPrice": "319",
"proComm": "1.47.5"
}, {
"spid": "04",
"imgUrl": "./imges/4c.webp",
"proName": " 米家空气净化器Pro H ",
"proPrice": "1699",
"proComm": "2.6"
}, {
"spid": "05",
"imgUrl": "./imges/5c.webp",
"proName": "8H乳胶弹簧静音床垫2 ",
"proPrice": "1699",
"proComm": "0.96"
}, {
"spid": "06",
"imgUrl": "./imges/6c.webp",
"proName": " 小米智能摄像机 云台版2K ",
"proPrice": "229",
"proComm": "18.2"
}, {
"spid": "07",
"imgUrl": "./imges/7c.webp",
"proName": " 小米智能多模网关",
"proPrice": "169",
"proComm": "8.1"
}, {
"spid": "08",
"imgUrl": "./imges/8c.webp",
"proName": " 8H乳胶床垫2 ",
"proPrice": "559",
"proComm": "1.9"
}, {
"spid": "09",
"imgUrl": "./imges/9c.webp",
"proName": " 小米米家智能开关 3个装 ",
"proPrice": "59",
"proComm": "6.4"
}, {
"spid": "10",
"imgUrl": "./imges/10c.webp",
"proName": " 全面屏电视E43K ",
"proPrice": "1399",
"proComm": "7.5"
}];
var oBox = document.getElementById("box");
var oCar = document.getElementById("car");
var oUl = document.getElementsByTagName("ul")[0];
for (var i = 0; i < aData.length; i++) {
var oLi = document.createElement("li");
var data = aData[i];
// console.log(data);
oLi.innerHTML += '<div class="pro_img"><img src="' + data["imgUrl"] + '" width="150" height="150"></div>';
oLi.innerHTML += '<h3 class="pro_name"><a href="#">' + data["proName"] + '</a></h3>';
oLi.innerHTML += '<p class="pro_price">' + data["proPrice"] + '元</p>';
oLi.innerHTML += '<p class="pro_rank">' + data["proComm"] + '万人好评</p>';
oLi.innerHTML += '<div class="add_btn">加入购物车</div>';
oUl.appendChild(oLi);
}
</script>
数组元素操作
var myCars=["Saab","Volvo","BMW"];
// 获取数组中元素的个数:
var len = myCars.length;
// 在结尾添加数组元素, 返回值为新数组长度
var newlen = myCars.push('zzzz');
// 在数组开头添加新元素, 返回值为新数组长度
var newlen = myCars.unshift('zzzz');
// 删除数组最后一个元素,返回值是删除的元素
var oldValue = myCars.pop();
// 删除数组第一个元素,返回值是删除的元素
var oldValue = myCars.shift();
// 更改元素
myCars[0] = 'newValue';
// 判断数组中是否包含某个元素,如果有返回索引,没有返回-1
var key = myCars.indexOf('Sabb');
//数组合并concat()
var arr=[1,2,3,50];
var s =[a,b,c,d,e];
var h =arr.concat(s);
//数组转字符串
var s =[a,b,c,d,e];
转为,隔开的字符串
var a= s.join(,);
数组整数求和
var arr =[1,3,5,6,9];
var n=0;
for(var i =0;i<arr.length;i++){
n+=arr[i];
}
console.log(n);
数组整数排序1
function com(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}
var box = [1, 2, 3, 4, 5, 15, 22, 55];
alert(box.sort(com)); //从小到大
alert(box.reverse()); //从大到小
数组中最大值
var box = [1, 2, 3, 4, 5, 15, 22, 55];
var n=0;
for(var i=0;i<box.length;i++){
if(n<arr[i]){
n=arr[i];
}
}
console.log(n);