Skip to content

Latest commit

 

History

History
52 lines (36 loc) · 1.81 KB

File metadata and controls

52 lines (36 loc) · 1.81 KB

Deep Copy Array

var copy = JSON.parse(JSON.stringify(array));

Copying an array of objects into another array in javascript (Deep Copy)

Simple array like [1, 2, 3]

var copy = array.slice()

中文比较

str1.localeCompare(str2, 'zh-CN')

js实现汉字中文排序的方法 例如:省市列表的排序

数组去重

function unique(arr) {
  return arr.filter(function(item, index, arr) {
    //当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
    return arr.indexOf(item, 0) === index;
  });
}
    var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
        console.log(unique(arr))
//[1, "true", true, 15, false, undefined, null, "NaN", 0, "a", {…}, {…}]

JavaScript数组去重(12种方法,史上最全)

Random String

// reference: https://attacomsian.com/blog/javascript-generate-random-string#:~:text=There%20are%20many%20ways%20available%20to%20generate%20a,a%20string%20and%20then%20remove%20the%20trailing%20zeros%3A
const randomString = (length = 8) => {
  // Declare all characters
  let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

  // Pick characers randomly
  let str = '';
  for (let i = 0; i < length; i++) {
      str += chars.charAt(Math.floor(Math.random() * chars.length));
  }

  return str;

};