🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 数组操作 * 创建数组 ~~~ let strArr = ["Alice", "Bob", "Cindy"]; let numArr = [1, 3, 5, 7, 9]; console.log(strArr); console.log(numArr); ~~~ * 访问和修改数组元素 ~~~ let strArr = ["Alice", "Bob", "Cindy"]; strArr[0] = "Dog"; // 将Alice修改为Dog console.log(strArr); ~~~ * 获取数组长度 ~~~ let strArr = ["Alice", "Bob", "Cindy"]; console.log(strArr.length); ~~~ * 遍历数组 ~~~ let strArr = ["Alice", "Bob", "Cindy"]; for (let i=0; i < strArr.length; i++) { console.log(strArr[i]); } ~~~ * 字符串转换数组 ~~~ let userName = "Alice, Bob, Cindy, Dog"; let strToArr = userName.split(","); console.log(strToArr); console.log(strToArr[0]); ~~~ * 数组转换字符串 ~~~ let arr = ["Alice", "Bob", "Cindy", "Dog"]; let arrToStr = arr.join(","); console.log(arrToStr); ~~~ * 添加和删除元素 ~~~ let arr = ["Alice", "Bob", "Cindy", "Dog"]; arr.push("Eddie"); // 在结尾添加 arr.unshift("Eddie");// 在开头添加 console.log(arr); arr.pop(); // 在结尾删除 arr.shift(); // 在开头删除 console.log(arr); ~~~