最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • js常用数组方法

    正文概述 掘金(bearWu)   2020-12-24   370

    常用数组

    最近经常忘记数组方法,今天就来整理一下

    1. Array.push(),方法将一个或多个元素添加到数组的末尾,并返回该数组的新长度。

    const animals = ['pigs', 'goats', 'sheep'];
    animals.push('chickens', 'cats', 'dogs');//["pigs", "goats", "sheep", "chickens", "cats", "dogs"]
    
    

    2. Array.pop(),方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。

    const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
    console.log(plants.pop()); // expected output: "tomato"
    console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
    

    3. Array.shift(),方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。

    const array1 = [1, 2, 3];
    const firstElement = array1.shift();
    console.log(array1); // expected output: Array [2, 3]
    console.log(firstElement); // expected output: 1
    

    4. Array.unshift(),方法将一个或多个元素添加到数组的开头,并返回该数组的新长度(该方法修改原有数组)。

    const array1 = [1, 2, 3];
    console.log(array1.unshift(4, 5));// expected output: 5
    console.log(array1);// expected output: Array [4, 5, 1, 2, 3]
    

    5. Array.concat(arr1,arr2...),方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

    const array1 = ['a', 'b', 'c'];
    const array2 = ['d', 'e', 'f'];
    const array3 = array1.concat(array2);
    console.log(array3);// expected output: Array ["a", "b", "c", "d", "e", "f"]
    

    6. Array.join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。

    const elements = ['Fire', 'Air', 'Water'];
    
    console.log(elements.join());// expected output: "Fire,Air,Water"
    console.log(elements.join(''));// expected output: "FireAirWater"
    console.log(elements.join('-'));// expected output: "Fire-Air-Water"
    

    7. Array.reverse(),方法将数组中元素的位置颠倒,并返回该数组。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。该方法会改变原数组。

    const array1 = ['one', 'two', 'three'];
    console.log('array1:', array1);// expected output: "array1:" Array ["one", "two", "three"]
    const reversed = array1.reverse();
    console.log('reversed:', reversed);// expected output: "reversed:" Array ["three", "two", "one"]
    // Careful: reverse is destructive -- it changes the original array.
    console.log('array1:', array1);
    // expected output: "array1:" Array ["three", "two", "one"]
    

    8. Array.sort(),方法用原地算法对数组的元素进行排序,并返回数组。默认排序顺序是在将元素转换为字符串,然后比较它们的UTF-16代码单元值序列时构建的

    const months = ['March', 'Jan', 'Feb', 'Dec'];
    months.sort();
    console.log(months);
    // expected output: Array ["Dec", "Feb", "Jan", "March"]
    
    const array1 = [1, 30, 4, 21, 100000];
    array1.sort();
    console.log(array1);
    // expected output: Array [1, 100000, 21, 30, 4]
    
    var numbers = [4, 2, 5, 1, 3];
    numbers.sort((a, b) => a - b);
    console.log(numbers);
    // [1, 2, 3, 4, 5]
    

    9.Array.map,方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。

    const array1 = [1, 4, 9, 16];
    const map1 = array1.map(x => x * 2);
    console.log(map1); // expected output: Array [2, 8, 18, 32]
    

    因为map生成一个新数组,当你不打算使用返回的新数组却使用map是违背设计初衷的,请用forEach或者for-of替代。你不该使用map: A)你不打算使用返回的新数组,或/且 B) 你没有从回调函数中返回值。

    1. Array.slice(start,end),方法返回一个新的数组对象,这一对象是一个由 begin 和 end 决定的原数组的浅拷贝(包括 begin,不包括end)。原始数组不会被改变。
    const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
    console.log(animals.slice(2));// expected output: Array ["camel", "duck", "elephant"]
    console.log(animals.slice(2, 4));// expected output: Array ["camel", "duck"]
    console.log(animals.slice(1, 5));// expected output: Array ["bison", "camel", "duck", "elephant"]
    

    slice(-2) 表示提取原数组中的倒数第二个元素到最后一个元素(包含最后一个元素)。

    11.Array.splice(index,howmany,arr1,arr2...), 方法通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。

    const months = ['Jan', 'March', 'April', 'June'];
    months.splice(1, 0, 'Feb');
    // inserts at index 1
    console.log(months);// expected output: Array ["Jan", "Feb", "March", "April", "June"]
    months.splice(4, 1, 'May');
    // replaces 1 element at index 4
    console.log(months);// expected output: Array ["Jan", "Feb", "March", "April", "May"]
    

    start指定修改的开始位置(从0计数)可负数,deleteCount整数,表示要移除的数组元素的个数,item1, item2, ...要添加进数组的元素,从start 位置开始。如果不指定,则 splice() 将只删除数组元素。

    12.Array.forEach,方法对数组的每个元素执行一次给定的函数。

    const array1 = ['a', 'b', 'c'];
    array1.forEach(element => console.log(element));
    // expected output: "a"
    // expected output: "b"
    // expected output: "c"
    
    function logArrayElements(element, index, array) {
      console.log('a[' + index + '] = ' + element);
    }
    
    // 注意索引 2 被跳过了,因为在数组的这个位置没有项
    [2, 5, , 9].forEach(logArrayElements);
    // logs:
    // a[0] = 2
    // a[1] = 5
    // a[3] = 9
    

    13.Array.filter(function),方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

    const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
    const result = words.filter(word => word.length > 6);
    console.log(result);
    // expected output: Array ["exuberant", "destruction", "present"]
    

    14.Array.every(function),方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

    const isBelowThreshold = (currentValue) => currentValue < 40;
    const array1 = [1, 30, 39, 29, 10, 13];
    console.log(array1.every(isBelowThreshold));
    // expected output: true
    

    15.Array.some(function),方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

    const array = [1, 2, 3, 4, 5];
    // checks whether an element is even
    const even = (element) => element % 2 === 0;
    console.log(array.some(even));
    // expected output: true
    

    16.Array.reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

    const array1 = [1, 2, 3, 4];
    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    // 1 + 2 + 3 + 4
    console.log(array1.reduce(reducer));
    // expected output: 10
    // 5 + 1 + 2 + 3 + 4
    console.log(array1.reduce(reducer, 5));
    // expected output: 15
    

    起源地下载网 » js常用数组方法

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元