GithubHelp home page GithubHelp logo

Array 对象方法整理 about blog HOT 5 OPEN

phoebeCodeSpace avatar phoebeCodeSpace commented on August 16, 2024
Array 对象方法整理

from blog.

Comments (5)

phoebeCodeSpace avatar phoebeCodeSpace commented on August 16, 2024

数组的深拷贝

  1. arr.slice
var arr = [1,2,3,4,5]
var arr2 = arr.slice(0)
arr[2] = 5
console.log(arr)
console.log(arr2)

参考:

http://blog.csdn.net/fungleo/article/details/54931379

from blog.

phoebeCodeSpace avatar phoebeCodeSpace commented on August 16, 2024

数组去重

  1. ES6
[...new Set([2,"12",2,12,1,2,1,6,12,13,6])]
//[2, "12", 12, 1, 6, 13]
  1. ES5 filter
    indexOf返回数组元素第一个位置。
[1,2,3,1,'a',1,'a'].filter(function(ele,index,array){
    return index===array.indexOf(ele)
})
  1. 新数组保存
  • 创建一个新的数组存放结果
  • 创建一个空对象
  • for循环时,每次取出一个元素与对象进行对比,如果这个元素不重复,则把它存放到结果数组中,同时把这个元素的内容作为对象的一个属性,并赋值为1,存入到第2步建立的对象中。
/*
  使用方法:
  var arr = [112,112,34,'你好',112,112,34,'你好','str','str1'];
  alert(arr.unique3());
*/

Array.prototype.unique = function(){
 var res = [];
 var json = {};
 for(var i = 0; i < this.length; i++){
  if(!json[this[i]]){
   res.push(this[i]);
   json[this[i]] = 1;
  }
 }
 return res;
}

from blog.

phoebeCodeSpace avatar phoebeCodeSpace commented on August 16, 2024

排序

数据按中文拼音首字母排序

var seatlist = [{id:1,name:'张三'},{id:2,name:'李四'},{id:3,name:'王五'}]
seatlist.sort((a,b)=>{
   return a.name.localeCompare(b.name, 'zh-Hans-CN', {sensitivity: 'accent'})
})

表格实现正序反序

arr.sort((a,b)=>{
   let flag = (Number(a) - Number(b)>=0 ? -1 : 1;
   if(sort==2){
     return flag;
   }else{
     return -flag;
   }
})

from blog.

phoebeCodeSpace avatar phoebeCodeSpace commented on August 16, 2024
    var obj={};
    var chinese=[], // 汉字
        english=[], // 字母
        special=[], // 特殊字符
        number=[];  // 数字

    //判断类型
    function chenkType(data) {
        const reNum = /^[0-9]/;                 // 数字校验规则
        const reEn = /^[a-zA-Z]/;               // 字母校验规则
        const reCh = /[^\u0000-\u00FF]/;        // 中文校验
        const type = {
            number: reNum,
            chinese: reCh,
            english: reEn
        }
        for (let d of data) {
            if (reNum.test(d)) {
                number.push(d);
            } else if (reEn.test(d)) {
                english.push(d);
            } else if (reCh.test(d)) {
                chinese.push(d);
            } else {
                special.push(d);
            }
        }
    }
    //汉字排序
    function pySegSort(arr) {
        var letters = "*abcdefghjklmnopqrstwxyz".split('');
        var zh = "阿八嚓哒妸发旮哈讥咔垃痳拏噢妑七呥扨它穵夕丫帀".split('');

        var segs = [];
        var curr;

        letters.forEach((letter,i)=>{
                curr = {letter, data:[]};  // 当前字母,对应数据
                arr.forEach((word)=>{
                if((!zh[i-1] || zh[i-1].localeCompare(word,"zh") <=0 ) && word.localeCompare(zh[i], "zh")==- 1) {
                        curr.data.push(word);
                    }
                })
                if(curr.data.length) {
                    segs.push(curr);
                    curr.data.sort((a,b)=>{a.localeCompare(b,"zh")});
                }
        });
        return segs;
    }


   //排序
    function sortAll(data) {
        // 类型归类
        chenkType(data);

        var temphanzi=pySegSort(chinese);
        var arr1 = [].concat(english,temphanzi)
            arr1.sort((a,b)=>{
                let c = a.letter || a,
                    d = b.letter || b;
                return c.localeCompare(d)
            })
        var arr2 = number;
            arr2.sort((a,b)=>{
                return a-b>0
            })
        var arr3 = special.sort((a,b)=>{
                return a.localeCompare(b)
        })
        var arr = arr1.concat(arr2,arr3);
        var result = [];
        arr.map((item)=>{
            result.push(item.data || item);
        })
        return result.reduce((previous, current) => previous.concat(current))
   };

console.log(sortAll(['amy','Amy','艾米','123','bubu','748','##','%^','李四','-##^','张三','赵亮']));
// ["艾米", "amy", "Amy", "bubu", "李四", "张三", "赵亮", "123", "748", "-##^", "##", "%^"]

from blog.

phoebeCodeSpace avatar phoebeCodeSpace commented on August 16, 2024

数组求最大最小值

function maxArr(arr){
    return Math.max.apply(null,arr);
}
function minArr(arr){
    return Math.min.apply(null,arr);
}

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.