汇总数组的方法

78 阅读3分钟

1、fliter过滤

注意:filter和find区别:filter返回的是数组,find返回的是对象

const products = [
    {
    name: "cucumber",
    type: "vegetable"
    },{
    name: "apple",
    type: "fruit"
    },{
    name: "orange",
    type: "fruit"
    }
]
const filters = products.filter(function(item) {
   return item.type == "fruit";
});

//结果[{name: "apple", type: "fruit"},{name: "orange", type: "fruit"}]

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

const users = [
{ name: "jack", age: 12 },
{ name: "alex", age: 15 },
{ name: "eva", age: 20 }
];
const user = users.find(function(item) {
 creturn (item.name = "eva");
});

//结果:{ name: "eva", age: 20 }

3、sort()

方法用就地( in-place )的算法对数组的元素进行排序,并返回数组。 sort 排序不一定是稳定的。默认排序顺序是根据字符串Unicode码点。 3.1、升序排列

const arr=[1,2,3,6,9,10,4,5];
arr.sort((a,b)=>{
        return a-b;  
 })  
 
//[1, 2, 3, 4, 5, 6, 9, 10]

3.2、降序排列

arr.sort((a,b)=>{
     return b-a
})
// [10, 9, 6, 5, 4, 3, 2, 1]

3.3、随机排序(1)

const arr1 = [1,2,3,4,5,6,7,8,9,10];
arr.sort(()=>{
        return Math.random() - 0.5;
})
console.log(arr);

//[1, 9, 5, 2, 3, 10, 6, 4]

3.4 随机排序(2)

const arr2 = [1,2,3,4,5,6,7,8,9,10];
function randSort1(arr){
    for(var i = 0,len = arr.length;i < len; i++ ){
            var rand = parseInt(Math.random()*len);
            var temp = arr[rand];
            arr[rand] = arr[i];
            arr[i] = temp;
    }
    return arr;
}
console.log(randSort1(arr2));
// [8, 6, 7, 1, 10, 9, 5, 4, 3, 2]

3.5 随机排序(3)

const arr3 = [1,2,3,4,5,6,7,8,9,10];
function randSort2(arr){
        var mixedArray = [];
        while(arr.length > 0){
                var randomIndex = parseInt(Math.random()*arr.length);
                mixedArray.push(arr[randomIndex]);
                arr.splice(randomIndex, 1);
        }
        return mixedArray;
}

console.log(randSort2(arr3));
// [1, 9, 5, 2, 8, 4, 10, 3, 7, 6]

4、删除数组元素

function remove(array, element) {
    const index = array.indexOf(element);
    array.splice(index, 1);
}
const arr4=[1,3,4,5,6];
remove(arr4,3);

//  [1,4,5,6];

5、push() 往数组的末尾添加元素或者添加多个元素

let arr4=[1,2,3,4];
arr4.push(5)

// [1,2,3,4,5]

arr4.push(6,7)

// [1,2,3,4,5,6,7]

6、pop() 从末尾删除数组元素,参数有没有都不影响结果

let arr5=[1,2,3,4];
arr5.pop()

//[1,2,3];

7、unshift() 数组的开头添加元素

let arr6=[1,2,3,4];
arr6.unshift(0)

//[0, 1, 2, 3, 4]
//arr6.unshift(0) 返回的是数组的长度

8、shift()从数组的开头删除元素

let arr6=[1,2,3,4];
arr6.shift()

// [2,3,4]
// arr6.shift() 数组对应0下标的元素

9、splice()数组元素的替换

  • 9.1、删除(只需要指定两个参数,第一是删除项所在位置,删除项的项数)
 let arr7=[1,2,3,4,5,6,7,8];
 arr7.splice(1,2);

//输出 [2, 3]
//arr7  [1, 4, 5, 6, 7, 8]
  • 9.2、 首先就是参数splice(起始位置,长度,替换的对象(替换的对象可以为多个));
let arr8=[1,2,3,9,8,-1,0,-4];
arr8.splice(0,5,'word')  

//输出  [1, 2, 3, 9, 8]
//arr8 ['word', -1, 0, -4]
  • 9.3、添加元素splice(起始位置,0,替换的对象,替换的对象,...(替换的对象可以为多个))
let arr9=[1,2,3,4,5,6]
arr9.splice(0,0,'word')

//arr9 ['word', 1, 2, 3, 4, 5, 6]

10、join()

方法用于把数组中的所有元素转换一个字符串。
元素是通过指定的分隔符进行分隔的。

let arr10=['w','o','r','l','d']
let str=arr10.join('')

// str: 'world'

11、reverse()用于颠倒数组中元素的顺序(改变原数组)

let arr10=['w','o','r','l','d']
arr10.reverse()

//  ['d', 'l', 'r', 'o', 'w']

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

let arr1 = [1,2,3],arr2 = [4,5,6]
let arrCopy1 = arr1.concat(arr2)
let arrCopy2 = arr1.concat(9,[11,13]);

//arrCopy1 [1, 2, 3, 4, 5, 6]
//arrCopy2  [1, 2, 3, 9, 11, 13]

13、fill() 【ES6 新增】改变了原数组

array.fill(value, start, end)

fill()方法能使用特定值填充数组中的一个或多个元素。

当只是用一个参数时,该方法会用该参数的值填充整个数组。

let arr1 = [1,2,3],arr2 = [4,5,6]
arr1.fill(6)
arr2.fill('hello',2,3)

//arr1  [6, 6, 6]
//arr2  [4, 5, 'hello']