一、常用的测试写法
//array的写法 var myArray = ["Apple", "Orange", "Banana"]; //一、正常循环写法如下: var fruitFinal3 = "" for (var i = 0; i < myArray.length; i++) { fruitFinal3 += myArray[i] + " "; } console.log("1. for loop Results: ", fruitFinal3); //二、用forEach写法如下: var fruitFinal4 = ""; myArray.forEach(myFunction); function myFunction(value) { fruitFinal4 += value + " "; } console.log("2. forEach Results: ", fruitFinal4);
测试结果如下:
二、不用看,请略过这些奇葩想法
//三、底下都不用看,是自己瞎写+瞎测试一些怪想法 console.log(myArray)//会显示出这是Array类型 //存疑写法?如下写法在页面上可以整体输出内容来, document.getElementById("demo3").innerHTML = myArray; //读取每个值,不要用这种写法,这种写法只打印出序号 var fruitFinal = "" for (const ele in myArray) { fruitFinal += ele+" "; } console.log("类似object写法1?: ", fruitFinal); //如果非要按上面写法,因为array也属于object的一种,上面可以改写成如下: var fruitFinal2 = "" for (const ele in myArray) { fruitFinal2 += myArray[ele] + " "; } console.log("类似object写法2?: ", fruitFinal2);
console.log 测试结果如下:
demo3页面的显示如下:
标签:console,log,myArray,用法,数组,var,ele,JS,写法 From: https://www.cnblogs.com/keeplearningandsharing/p/18361507