1 Star 0 Fork 0

math.most/js_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
011.0数组.js 3.49 KB
一键复制 编辑 原始数据 按行查看 历史
math.most 提交于 2021-09-23 22:48 +08:00 . 继续补充
/**
* 数组
*/
// 1. 四个基本方法 push pop unshift shift
var newList = ['孙悟空', '白晶晶', '法海'];
// ---> push向数组末尾添加一个或多个元素并返回数组的新长度
newList.push('唐僧');
var result = newList.push('白骨精', '如来');
console.log(newList); // ['孙悟空', '白晶晶', '法海', '唐僧', '白骨精', '如来']
console.log(result); // 6
// ---> pop删除数组最后一个元素, 并将被删除的元素返回
var pop = newList.pop();
console.log(newList); // ['孙悟空', '白晶晶', '法海', '唐僧', '白骨精']
console.log(pop); // 如来
// ---> unshift 向数组开头添加一个或多个元素并返回数组的新长度
newList.unshift('牛魔王');
var result = newList.unshift('二郎神', '玉帝');
console.log(newList); // ['二郎神', '玉帝', '牛魔王', '孙悟空', '白晶晶', '法海', '唐僧', '白骨精']
console.log(result); // 8
// ---> shift 删除数组第一个元素并将被删除的元素返回
var result = newList.shift();
console.log(result); // 二郎神
// 2. 数组遍历
// ---> for
for(var per of newList){
console.log(per); // 玉帝 牛魔王 孙悟空 白晶晶 法海 唐僧 白骨精
};
// ---> forEach 只支持IE8以上的浏览器
var lst = ['1', '2', '3', '4', '5', '6', '7', '8', '9'];
lst.forEach(ls => {
console.log(ls); // ['1', '2', '3', '4', '5', '6', '7', '8', '9']
});
// 3. 其他方法
// ---> slice 从一个数组中返回选定的元素: 切片
// [start, end)
// 该方法不会改变原数组,只是将结果重新追加到一个新的列表中
var slice_res = newList.slice(0, 2);
console.log(slice_res); // [ '玉帝', '牛魔王' ]
var slice_res = newList.slice(0, -2);
console.log(slice_res); // [ '玉帝', '牛魔王', '孙悟空', '白晶晶', '法海' ]
// ---> splice 删除元素并向数组添加新元素
// [start, count, newElement] start 删除的索引, count 删除的个数, newElement 自动插入到开始位置索引前边
// 该方法会改变原数组
var splice_res = lst.splice(0, 3);
console.log(splice_res); // [ '1', '2', '3' ]
console.log(lst); // [ '4', '5', '6', '7', '8', '9' ]
var splice_new_element_res = lst.splice(1, 3, '10', '12', '14', '16');
console.log(splice_new_element_res); // [ '5', '6', '7' ]
console.log(lst); // [ '4', 10', '12', '14', '16', '7', '8', '9' ]
// ---> 数组去重
// indexOf
function uniq(array){
var temp = []; //一个新的临时数组
for(var i = 0; i < array.length; i++){
if(temp.indexOf(array[i]) == -1){
temp.push(array[i]);
}
}
return temp;
}
var aList = ['1', '2', '2', '3', '6', '6', '6', '8', '9'];
console.log(uniq(aList)); // [ '1', '2', '3', '6', '8', '9' ]
// 将字符串转换为数组
let str = "hello world"
console.log("将字符串转换为数组:" + Array.from(str)) // 将字符串转换为数组:h,e,l,l,o, ,w,o,r,l,d
// set: es6新特性(去重)
function uniq2(arr) {
if (!Array.isArray(arr)) {
return;
}
return Array.from(new Set(arr))
}
console.log(uniq2(aList)); // [ '1', '2', '3', '6', '8', '9' ]
// ---> 数组转化为对象
// 普通
var obj = {}
var arr = ["1", "2", "3"]
for (var key in arr) {
obj[key] = arr[key]
}
console.log(obj) // {0: 1, 1: 2, 2: 3}
// 简单快速的方法
const arr2 = [1, 2, 3]
const obj2 = {...arr2}
console.log(obj2) // {0: 1, 1: 2, 2: 3}
// ---> 拷贝数组
// 1. Array.slice
const arr3 = [1, 2, 3, 4, 5, 6];
const copyArr = arr3.slice();
// 2. 展开操作符
const arr4 = [1, 2, 3, 4, 5, 6];
const copyArr2 = [...arr4];
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mathmost/js_study.git
git@gitee.com:mathmost/js_study.git
mathmost
js_study
js_study
master

搜索帮助