zoukankan      html  css  js  c++  java
  • JavaScript常用数组操作方法

    ES5操作数组的方法

    1、concat()

    concat() 方法用于连接两个或多个数组。该方法不会改变现有的数组,仅会返回被连接数组的一个副本。

    1. var arr1 = [1,2,3];

    2. var arr2 = [4,5];

    3. var arr3 = arr1.concat(arr2);

    4. console.log(arr1); //[1, 2, 3]

    5. console.log(arr3); //[1, 2, 3, 4, 5]

    2、join()

    join() 方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的,默认使用','号分割,不改变原数组。

    1. var arr = [2,3,4];

    2. console.log(arr.join());  //2,3,4

    3. console.log(arr);  //[2, 3, 4]

    3、push()

    push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。末尾添加,返回的是长度,会改变原数组。

    1. var a = [2,3,4];

    2. var b = a.push(5);

    3. console.log(a);  //[2,3,4,5]

    4. console.log(b);  //4

    5. push方法可以一次添加多个元素push(data1,data2....)

    4、pop()

    pop() 方法用于删除并返回数组的最后一个元素。返回最后一个元素,会改变原数组。

    1. var arr = [2,3,4];

    2. console.log(arr.pop()); //4

    3. console.log(arr);  //[2,3]

    5、shift()

    shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。返回第一个元素,改变原数组。

    1. var arr = [2,3,4];

    2. console.log(arr.shift()); //2

    3. console.log(arr);  //[3,4]

    6、unshift()

    unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。返回新长度,改变原数组。

    1. var arr = [2,3,4,5];

    2. console.log(arr.unshift(3,6)); //6

    3. console.log(arr); //[3, 6, 2, 3, 4, 5]

    4. tip:该方法可以不传参数,不传参数就是不增加元素。

    7、slice()

    返回一个新的数组,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素。返回选定的元素,该方法不会修改原数组。

    1. var arr = [2,3,4,5];

    2. console.log(arr.slice(1,3));  //[3,4]

    3. console.log(arr);  //[2,3,4,5]

    8、splice()

    splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素。如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组。splice() 方法会直接对数组进行修改。

    1. var a = [5,6,7,8];

    2. console.log(a.splice(1,0,9)); //[]

    3. console.log(a);  // [5, 9, 6, 7, 8]

    4. var b = [5,6,7,8];

    5. console.log(b.splice(1,2,3));  //[6, 7]

    6. console.log(b); //[5, 3, 8]

    9、substring() 和 substr()

    相同点:如果只是写一个参数,两者的作用都一样:都是是截取字符串从当前下标以后直到字符串最后的字符串片段。
    substr(startIndex);
    substring(startIndex);

    1. var str = '123456789';

    2. console.log(str.substr(2));    //  "3456789"

    3. console.log(str.substring(2)) ;//  "3456789"

    不同点:第二个参数
    substr(startIndex,lenth): 第二个参数是截取字符串的长度(从起始点截取某个长度的字符串);
    substring(startIndex, endIndex): 第二个参数是截取字符串最终的下标 (截取2个位置之间的字符串,‘含头不含尾’)。

    1. console.log("123456789".substr(2,5));    //  "34567"

    2. console.log("123456789".substring(2,5)) ;//  "345"

    10、sort 排序

    按照 Unicode code 位置排序,默认升序

    1. var fruit = ['cherries', 'apples', 'bananas'];

    2. fruit.sort(); // ['apples', 'bananas', 'cherries']

    3. var scores = [1, 10, 21, 2];

    4. scores.sort(); // [1, 10, 2, 21]

    11、reverse()

    reverse() 方法用于颠倒数组中元素的顺序。返回的是颠倒后的数组,会改变原数组。

    1. var arr = [2,3,4];

    2. console.log(arr.reverse()); //[4, 3, 2]

    3. console.log(arr);  //[4, 3, 2]

    12、indexOf 和 lastIndexOf

    都接受两个参数:查找的值、查找起始位置
    不存在,返回 -1 ;存在,返回位置。indexOf 是从前往后查找, lastIndexOf 是从后往前查找。
    indexOf

    1. var a = [2, 9, 9];

    2. a.indexOf(2); // 0

    3. a.indexOf(7); // -1

    4. if (a.indexOf(7) === -1) {

    5.  // element doesn't exist in array

    6. }

    lastIndexOf

    1. var numbers = [2, 5, 9, 2];

    2. numbers.lastIndexOf(2);     // 3

    3. numbers.lastIndexOf(7);     // -1

    4. numbers.lastIndexOf(2, 3);  // 3

    5. numbers.lastIndexOf(2, 2);  // 0

    6. numbers.lastIndexOf(2, -2); // 0

    7. numbers.lastIndexOf(2, -1); // 3

    13、every

    对数组的每一项都运行给定的函数,每一项都返回 ture,则返回 true

    1. function isBigEnough(element, index, array) {

    2.  return element < 10;

    3. }    

    4. [2, 5, 8, 3, 4].every(isBigEnough);   // true

    14、some

    对数组的每一项都运行给定的函数,任意一项都返回 ture,则返回 true

    1. function compare(element, index, array) {

    2.  return element > 10;

    3. }    

    4. [2, 5, 8, 1, 4].some(compare);  // false

    5. [12, 5, 8, 1, 4].some(compare); // true

    15、filter

    对数组的每一项都运行给定的函数,返回 结果为 ture 的项组成的数组

    1. var words = ["spray", "limit", "elite", "exuberant", "destruction", "present", "happy"];

    2. var longWords = words.filter(function(word){

    3.  return word.length > 6;

    4. });

    5. // Filtered array longWords is ["exuberant", "destruction", "present"]

    16、map

    对数组的每一项都运行给定的函数,返回每次函数调用的结果组成一个新数组

    1. var numbers = [1, 5, 10, 15];

    2. var doubles = numbers.map(function(x) {

    3.   return x * 2;

    4. });

    5. // doubles is now [2, 10, 20, 30]

    6. // numbers is still [1, 5, 10, 15]

    17、forEach 数组遍历

    1. const items = ['item1', 'item2', 'item3'];

    2. const copy = [];    

    3. items.forEach(function(item){

    4.  copy.push(item)

    5. });

    ES6新增新操作数组的方法

    1、find():

    传入一个回调函数,找到数组中符合当前搜索规则的第一个元素,返回它,并且终止搜索。

    1. const arr = [1, "2", 3, 3, "2"]

    2. console.log(arr.find(n => typeof n === "number")) // 1

    2、findIndex():

    传入一个回调函数,找到数组中符合当前搜索规则的第一个元素,返回它的下标,终止搜索。

    1. const arr = [1, "2", 3, 3, "2"]

    2. console.log(arr.findIndex(n => typeof n === "number")) // 0

    3、fill():

    用新元素替换掉数组内的元素,可以指定替换下标范围。

    1. arr.fill(value, start, end)

    4、copyWithin():

    选择数组的某个下标,从该位置开始复制数组元素,默认从0开始复制。也可以指定要复制的元素范围。

    1. arr.copyWithin(target, start, end)

    2. const arr = [1, 2, 3, 4, 5]

    3. console.log(arr.copyWithin(3))

    4. // [1,2,3,1,2] 从下标为3的元素开始,复制数组,所以4, 5被替换成1, 2

    5. const arr1 = [1, 2, 3, 4, 5]

    6. console.log(arr1.copyWithin(3, 1))

    7. // [1,2,3,2,3] 从下标为3的元素开始,复制数组,指定复制的第一个元素下标为1,所以4, 5被替换成2, 3

    8. const arr2 = [1, 2, 3, 4, 5]

    9. console.log(arr2.copyWithin(3, 1, 2))

    10. // [1,2,3,2,5] 从下标为3的元素开始,复制数组,指定复制的第一个元素下标为1,结束位置为2,所以4被替换成2

    5、from

    将类似数组的对象(array-like object)和可遍历(iterable)的对象转为真正的数组

    1. const bar = ["a", "b", "c"];

    2. Array.from(bar);

    3. // ["a", "b", "c"]

    4. Array.from('foo');

    5. // ["f", "o", "o"]

    6、of

    用于将一组值,转换为数组。这个方法的主要目的,是弥补数组构造函数 Array() 的不足。因为参数个数的不同,会导致 Array() 的行为有差异。

    1. Array() // []

    2. Array(3) // [, , ,]

    3. Array(3, 11, 8) // [3, 11, 8]

    4. Array.of(7);       // [7]

    5. Array.of(1, 2, 3); // [1, 2, 3]

    6. Array(7);          // [ , , , , , , ]

    7. Array(1, 2, 3);    // [1, 2, 3]

    7、entries() 返回迭代器:返回键值对

    1. //数组

    2. const arr = ['a', 'b', 'c'];

    3. for(let v of arr.entries()) {

    4.  console.log(v)

    5. }

    6. // [0, 'a'] [1, 'b'] [2, 'c']

    7. //Set

    8. const arr = new Set(['a', 'b', 'c']);

    9. for(let v of arr.entries()) {

    10.  console.log(v)

    11. }

    12. // ['a', 'a'] ['b', 'b'] ['c', 'c']

    13. //Map

    14. const arr = new Map();

    15. arr.set('a', 'a');

    16. arr.set('b', 'b');

    17. for(let v of arr.entries()) {

    18.  console.log(v)

    19. }

    20. // ['a', 'a'] ['b', 'b']

    8、values() 返回迭代器:返回键值对的value

    1. //数组

    2. const arr = ['a', 'b', 'c'];

    3. for(let v of arr.values()) {

    4.  console.log(v)

    5. }

    6. //'a' 'b' 'c'

    7. //Set

    8. const arr = new Set(['a', 'b', 'c']);

    9. for(let v of arr.values()) {

    10.  console.log(v)

    11. }

    12. // 'a' 'b' 'c'

    13. //Map

    14. const arr = new Map();

    15. arr.set('a', 'a');

    16. arr.set('b', 'b');

    17. for(let v of arr.values()) {

    18.  console.log(v)

    19. }

    20. // 'a' 'b'

    9、keys() 返回迭代器:返回键值对的key

    1. //数组

    2. const arr = ['a', 'b', 'c'];

    3. for(let v of arr.keys()) {

    4.  console.log(v)

    5. }

    6. // 0 1 2

    7. //Set

    8. const arr = new Set(['a', 'b', 'c']);

    9. for(let v of arr.keys()) {

    10.  console.log(v)

    11. }

    12. // 'a' 'b' 'c'

    13. //Map

    14. const arr = new Map();

    15. arr.set('a', 'a');

    16. arr.set('b', 'b');

    17. for(let v of arr.keys()) {

    18.  console.log(v)

    19. }

    20. // 'a' 'b'

    10、includes

    判断数组中是否存在该元素,参数:查找的值、起始位置,可以替换 ES5 时代的 indexOf 判断方式。indexOf 判断元素是否为 NaN,会判断错误。

    1. var a = [1, 2, 3];

    2. a.includes(2); // true

    3. a.includes(4); // false

    END

  • 相关阅读:
    Python ctypes调用clib代码示例
    一点利用lme4包进行BLUP/BLUE计算的DEMO
    文献阅读 | Identifying barley pan-genome sequence anchors using genetic mapping and machine learning
    文献阅读 | Plant-ImputeDB: an integrated multiple plant reference panel database for genotype imputation
    文献阅读 | Genetic Diversity, Pedigree Relationships, and A Haplotype-Based DNA Fingerprinting System of Red Bayberry Cultivars
    文献阅读 | The Power of Inbreeding: NGS-Based GWAS of Rice Reveals Convergent Evolution during Rice Domestication
    文献阅读 | Worldwide phylogeography and history of wheat genetic diversity
    文献阅读 | RPAN: rice pan-genome browser for ∼3000 rice genomes
    tfidf代码简单实现
    conda 安装 graph-tool, 无需编译
  • 原文地址:https://www.cnblogs.com/guchengnan/p/9699244.html
Copyright © 2011-2022 走看看