var ar=[112,44,55,66,77,88,99,'00',77]; var ar1=['ddd','fff','ggg']; //concat() 拼接一個或多個數組; //console.log(ar.concat(['a','b','c'])) console.log(ar.concat(ar1)); //push() 從數組后面添加元素 ar1.push('hhh',456,'fdsfdsa'); console.log(ar1) //unshift() 從數組前面添加元素; ar1.unshift('ttt',111); console.log(ar1) //shift() 刪除數組的第一個元素 ar.shift() console.log(ar) //pop() 刪除數組的最后一個元素 ar.pop() console.log(ar) //slice() 從已有數組中返回選定的元素(-1指最后一個元素) console.log(ar.slice(3,-1)) //indexOf() 返回一個指定的字符串值第一次出現的位置(下標值); console.log(ar1.indexOf('fff')) //lastindexOf() 返回一個指定的字符串值最后出現的位置; console.log(ar.lastIndexOf(99))
//splice() 從數組中刪除元素,並添加元素 var ar2=[555,666,777,888,999,000] //ar2.splice(1,2,'33'); ar2.splice(1,1,'ddd','ggg') console.log(ar2) //join() 里面如果什么都不填,則以逗號分割,否則以填入內容分割; console.log(ar2.join(' ')); //reverse() 反轉數組 // console.log(ar2.reverse()); //forEach() 遍歷數組 ele表示元素的值,index表示下標; ar2.forEach(function(ele,index){ document.write(ele+'<br/>') })