zoukankan      html  css  js  c++  java
  • 常用数组方法操作大全!!!!!【数组复制,数组去重,数组添加元素】

     
    1、数组长度length。
    2、在数组末尾加元素(一个或则多个都行)push;arr.push("1","2")
    3、在数组末尾删除一个元素,并返回这个被删除的元素pop;week.pop()
    4、在数组开头加元素(一个或则多个都行)unshift;arr.unshift("1","2")
    5、在数组头部删除一个元素,并返回这个被删除的元素;week.shift()
    6、删除数组任意位置且任意数量的元素splice;
    arr.splice(index, n), 从第index个元素开始(包含index),删除n个元素,并返回这个被删除元素
    7、检查数组是否包含指定的值includes;arr.includes("a"),有true,没就false.
    8、将数组用value连接为字符串join;arr.join(',');【不改变原数组】
    let a = [1, 2, 3, 4, 5]
    let res = a.join(',')
    console.log(res) // '1,2,3,4,5'
    9、反转数组reverse【(会改变原数组)】
    let a = [1, 2, 3, 4, 5]
    let res = a.reverse()
    console.log(res) // [5, 4, 3, 2, 1]
    console.log(a) // [5, 4, 3, 2, 1]
    10、截取指定位置的数组slice(start[包含],end(不包含))【不改变原数组】【可为负数,表示倒着截取】
    let a = [1, 2, 3, 4, 5]
    let result = a.slice(2, 4)
    console.log(result) // [3, 4]
    11、对数组元素进行排序sort【会改变原数组】
    按照字符编码的顺序进行排序。要实现这一点,首先应把数组的元素都转换成字符串(如有必要),以便进行比较。
    要排数字的话需要手写函数:
    function sortNumber(a,b)
    {
    return a - b
    }
    let arr=["1","2","5"]
    arr.sort(sortNumber)
    12、将数组中的元素用逗号拼接成字符串toString【不改变原数组】
    let result = a.toString()
    console.log(result) // 1,2,3,4,5
    13、从索引为0开始,检查数组是否包含value,有则返回匹配到的第一个索引,没有返回-1,indexOf【不改变原数组】 a.indexOf(2)
    14、从最后的索引开始,检查数组是否包含value,有则返回匹配到的第一个索引,没有返回-1(不改变原数组)lastIndexOf
    15、将数组和/或值连接成新数组concat【不改变原数组】
    let a = [1, 2], b = [3, 4], c = 5
    let result = a.concat(b, c)
    console.log(result) // [1, 2, 3, 4, 5]
    16、去重
    (1)for循环加indexof===-1就push进新数组【新数组为去重后的数组】
    (2)数组下标判断法 for循环加indexof,push.
    if (arr.indexOf(arr[i]) === i) {
          newArr.push(arr[i])
        }
    (3)es6 Set去重(无法去掉“{}”空对象)
    const arr = ['张三','张三','三张三']
    let set = new Set(arr); // set 自带去重
    // Set { '张三', '三张三' }
    (4)利用filter加indexOf
      (5)includes
      (6)双层循环for,splice去重【es5最常用】
    function unlink(arr) {
        for (var i = 0; i < arr.length; i++) {    // 首次遍历数组
            for (var j = i + 1; j < arr.length; j++) {   // 再次遍历数组
                if (arr[i] == arr[j]) {          // 判断连个值是否相等
                    arr.splice(j, 1);           // 相等删除后者
                    j--;
                }
            }
        }
        return arr
    }
    (7)reduce((当前,下一个)=>{},初始值)
    let person = [
         {id: 0, name: "小明"},
         {id: 1, name: "小张"}】
    person = person.reduce((cur,next) => {
        obj[next.id] ? "" : obj[next.id] = true && cur.push(next);
        return cur;
    },[]) //设置cur默认类型为数组,并且初始值为空的数组

  • 相关阅读:
    bzoj 1853: [Scoi2010]幸运数字 容斥
    bzoj 3545&&3551: [ONTAK2010]Peaks &&加强版 平衡树&&并查集合并树&&主席树
    bzoj 2331: [SCOI2011]地板 插头DP
    bzoj 3669: [Noi2014]魔法森林 动态树
    bzoj 2734: [HNOI2012]集合选数 状压DP
    bzoj 3751: [NOIP2014]解方程 同余系枚举
    bzoj 2594: [Wc2006]水管局长数据加强版 动态树
    bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树
    bzoj 2209: [Jsoi2011]括号序列 splay
    bzoj 1223: [HNOI2002]Kathy函数 数位DP 高精度
  • 原文地址:https://www.cnblogs.com/jundongsheng/p/13617765.html
Copyright © 2011-2022 走看看