开发面试题 IntelliJ IDEA 分布式调度 树莓派USB 阿里云 ISP debugging testing curl woocommerce ros 打印 ide Normalizecss vue学习 网页后台模板 ios视频教程 easyui视频 ie内核浏览器怎么设置 java常用的包 string转16进制 quartz配置 hbuilder插件 cad正在执行命令 mysql插入 python中文手册 python循环语句 java程序实例 java中泛型 java写入txt java数组 java的框架 java架构 内存整理软件 html5网页制作 js格式化时间 dvwa安装教程 vfloppy 迅雷去广告版 win10有几个版本 高通cpu排行
当前位置: 首页 > 学习教程  > 编程语言

数组的一些简单操作

2020/8/11 20:57:31 文章标签:

数组的一些相关操作

1.普通数组

(1)普通数组交集、并集、差集、补集、去重、排序

  let arr1 = [1, 2, 3, 4, 5 , 8 ,9];
    let arr2 = [4, 5, 6, 7, 8, 9]; 
    let arr3 = arr1.filter(function (val) { return arr2.indexOf(val) > -1 }); // 交集
    let arr4 = arr1.concat(arr2.filter(v => !arr1.includes(v)));//并集
    let arr5 = arr1.filter(item => !new Set(arr2).has(item));//差集
    let arr6 =Array.from(new Set(arr1.concat(arr2).filter(v => !new Set(arr1).has(v) || !new Set(arr2).has(v))));//补集(两个数组各自没有的集合)
    //数组去重
    let arr7 = Array.from(new Set([1,2,3,3,4,5,4,6,5]));
    let arr8 = [...new Set([1,2,3,3,4,5,4,6,5])];
    //数组排序 
    let arr9 = [2,1,3,4,7,6].sort((a,b) => a - b);//升序
    let arr10 = [2,1,3,4,7,6].sort((a,b) => b - a);//降序
    console.log(arr3);// [5, 8, 9]
    console.log(arr4);//[1, 2, 3, 4, 5, 8, 9, 6, 7]
    console.log(arr5);//[1, 2, 3]
    console.log(arr6);//[1, 2, 3, 6, 7]
    console.log(arr7);//[1, 2, 3, 4, 5, 6]
    console.log(arr8);//[1, 2, 3, 4, 5, 6]
    console.log(arr9);//[1, 2, 3, 4, 5, 6]
    console.log(arr10);//[7, 6, 4, 3, 2, 1]

(2)数组最大值、求和、合并、是否包含元素、是否每个元素都满足条件、 数组转对象

// 最大值
    var arr = [1,3,2,6,34,12,993,12,321,2]; 
    console.log(Math.max(...arr));//993
    console.log(Math.max.apply(this , arr));//993
    console.log(arr.reduce((prev , cur,curIndex,arr) => {
      return Math.max(prev,cur);
    }));//993
 // 求和
    var arr = [1,2,3,4,5];
    console.log(arr.reduce((prev , cur) => {
      return prev+cur;
    }));//15
 // 数组合并
    var arr = [1,2,3];
    var arr1 = [6,7,8];
    var arr2 = arr.concat(arr1);
    var arr3 = [...arr,...arr1];
    console.log(arr2);//[1, 2, 3, 6, 7, 8]
    console.log(arr3);//[1, 2, 3, 6, 7, 8]
  // 数组是否包含元素
   let arr = [1,2,3,4,5,6];
   console.log(arr.includes(2));//true ,不存在返回false
   console.log(arr.indexOf(4));//1 存在返回1,不存在返回-1
   console.log(arr.find((item) => item == 4));//2 存在返回元素  不存在返回undefined
   console.log(arr.findIndex((item) => item == 6));//1 存在返回1,不存在返回-1
  // 判断数组是否每一个元素都满足条件,这里使用的是every()
  console.log([4, 12, 3].every(item => { return item > 2 }));//true
  console.log([4, 1, 3].every(item => { return item > 2 }));//false

(3) 数组转对象

// 数组转对象
  let arr = ['小明','小红','小刚'];
  let ages = [10, 20, 30];
  let obj = arr.map((item,index) => {
        return {name:item ,age:ages[index]}
  });
  console.log(obj);//[{name: "小明", age: 10}, {name: "小红", age: 20}, {name: "小刚", age: 30}]

2.数组对象

(1)数组对象将某个属性抽取成数组

let arr = [{"name":"fang","age":123},
                {"name":"fang2","age":2},
                {"name":"fang3","age":3},
                {"name":"fang4","age":4},
                {"name":"fang5","age":223},];
    let arr1 = arr.map(item => item.age);
    console.log(arr1);//arr1 = [123,2,3,4,223];

(2)交集

 //  数组对象交集
let arr1 = [{name: "小明", age: 10}, {name: "小绿", age: 20}, {name: "小刚", age: 30}];
let arr2 = [{name: "小明", age: 10},{name: "小黄", age: 10}, {name: "小红", age: 20},{name: "小黑", age: 20}, {name: "小刚", age: 30}];
let result = arr2.filter(function (v) {
  return arr1.some(n => JSON.stringify(n) === JSON.stringify(v))
})
console.log(result); // [{name: "小明", age: 10}, {name: "小刚", age: 30}];

(3)并集

//并集
let arr1 = [{name: "小明", age: 10}, {name: "小绿", age: 20}, {name: "小刚", age: 30}];
let arr2 = [{name: "小明", age: 10},{name: "小黄", age: 10}, {name: "小红", age: 20},{name: "小黑", age: 20}, {name: "小刚", age: 30}];
let arr3 = arr1.concat(arr2);//合并数组
let result = [];
let obj = [];
result = arr3.reduce((prev , cur , index, arr) => {
  obj[cur.name] ? '':obj[cur.name] = true && prev.push(cur);//根据某个属性来去重,只能根据主键来去重,无法确定去除的对象是否完全相同。
  console.log("prev==",prev);
  return prev;
},[]);
console.log(result);

(4)差集

//  差集
let arr1 = [{name: "小明", age: 10}, {name: "小绿", age: 20}, {name: "小刚", age: 30}];
let arr2 = [{name: "小明", age: 10},{name: "小黄", age: 10}, {name: "小红", age: 20},{name: "小黑", age: 20}, {name: "小刚", age: 30}];
let result = arr2.filter(function (v) {//arr2将arr1中存在的相同对象减去
  return arr1.every(n => JSON.stringify(n) !== JSON.stringify(v))
})
console.log(result);//[{name: "小黄", age: 10}, {name: "小红", age: 20},{name: "小黑", age: 20}];

(5)补集

// 补集 --去除两个数组相同的项
let arr1 = [{name: "小明", age: 10}, {name: "小绿", age: 20}, {name: "小刚", age: 30}];
let arr2 = [{name: "小明", age: 10},{name: "小黄", age: 10}, {name: "小红", age: 20},{name: "小黑", age: 20}, {name: "小刚", age: 30}];
let arr3 = arr1.concat(arr2);
let result = arr3.filter(function (v) {
  return arr1.every(n => JSON.stringify(n) !== JSON.stringify(v)) || arr2.every(n => JSON.stringify(n) !== JSON.stringify(v))
})
console.log(result);

(6) 去重

//  去重
let arr1 = [{name: "小明", age: 10}, {name: "小绿", age: 20},{name: "小明", age: 10}, {name: "小刚", age: 30}, {name: "小刚", age: 30}];
let arr2 = [];
arr1.forEach(item=>{
    !arr2.some(v => JSON.stringify(v) === JSON.stringify(item)) && arr2.push(item)
 });
 console.log(arr2);//[{name: "小明", age: 10}, {name: "小绿", age: 20}, {name: "小刚", age: 30}]

(7)排序

// 排序
let arr1 = [{name: "小绿", age: 20},{name: "小明", age: 10}, {name: "小刚", age: 30}];
let arr2 = arr1.sort((a,b) => { return a.age - b.age});//升序
let arr3 = arr1.sort((a,b) => { return b.age - a.age});//降序
console.log(arr2);//[{name: "小明", age: 10},{name: "小绿", age: 20}, {name: "小刚", age: 30}]
console.log(arr3);//[{name: "小刚", age: 30},{name: "小绿", age: 20}, {name: "小明", age: 10}]

总结:都是一些简单的操作,写这篇博客只是为了方便自己查阅。


本文链接: http://www.dtmao.cc/news_show_100443.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?