let array1 = [{name:'黄老板',age:18},{name:'曹操',age:19},{name:'关翔',age:29}];
//添加
//1.追加
array1.push({name:'黄生',age:100});
//2.追加多个 arrayObject.concat(arrayX,arrayX,......,arrayX)
//返回一个新的数组
let temArray = [{name:'a',age:19},{name:'b',age:19}];
array1 = array1.concat(temArray);
console.log("array1",array1);
//3. 向数组前面加数据
array1.unshift({name:'李白',age:100});
//删除
//1. 0 表示开始角标 , 1 代表数据
array1.splice(0, 1);
console.log("array1",array1);
//1.filter 过滤
/**
* 可以不写return
* current 当前元素
* index
* self 当前对象 array1
*/
let filterDatas = array1.filter((current,index,self)=> current.age>=19);
console.log("filterDatas",filterDatas);
let filterDatas2 = array1.filter((current,index,self)=>{
//带 {} 要写return
return current.age>=19;
});
console.log("filterDatas2",filterDatas2);
//只写current
let filterDatas3 = array1.filter((current)=>current.age>=19);
console.log("filterDatas3",filterDatas3);
// map 转换
let mapDatas = array1.map((current)=>current.name);
console.log("mapDatas",mapDatas);//["黄老板", "曹操", "关翔"]
// 反转
console.log(mapDatas.reverse());
JS中数组常见操作
最新推荐文章于 2024-06-10 17:21:12 发布