美文网首页
工作中遇到的数组技巧

工作中遇到的数组技巧

作者: 小卟啾 | 来源:发表于2021-12-31 09:38 被阅读0次
    来自公众号:前端Q
    原文:https://www.blog.duomly.com/13-useful-javascript-array-tips-and-tricks-you-should-know/

    1. 数组去重

    var fruits = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"];
     
    // First method
    var uniqueFruits = Array.from(newSet(fruits));
    console.log(uniqueFruits);
    // returns ["banana", "apple", "orange", "watermelon", "grape"]
    
    // Second method
    var uniqueFruits2 = […newSet(fruits)];
    console.log(uniqueFruits2);
    // returns ["banana", "apple", "orange", "watermelon", "grape"]
    

    2. 替换数组中的特定值

    有时在创建代码时需要替换数组中的特定值,有一种很好的简单的方法可以做到这一点,我们可以使用.splice(start、valueToRemove、valueToAdd),并将所有三个参数传递给它,这些参数可以指定我们希望从哪里开始修改、希望修改多少值和新值。
    var fruits = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"];
    fruits.splice(0, 2, "potato", "tomato");
    console.log(fruits);
    // returns ["potato", "tomato", "orange", "watermelon", "apple", "orange", "grape", "apple"]
    

    3. 没有map()的映射数组

    var friends = [
        { name: "John", age: 22 },
        { name: "Peter", age: 23 },
        { name: "Mark", age: 24 },
        { name: "Maria", age: 22 },
        { name: "Monica", age: 21 },
        { name: "Martha", age: 19 },
    ]
     
    
    var friendsNames = Array.from(friends, ({name}) => name);
    console.log(friendsNames);
    // returns ["John", "Peter", "Mark", "Maria", "Monica", "Martha"]
    

    4.空数组

    var fruits = ["banana", "apple", "orange", "watermelon", "apple", "orange", "grape", "apple"];
     
    
    fruits.length = 0;
    console.log(fruits);
     // returns []
    

    5.将数组转换为对象

    var fruits = ["banana", "apple", "orange", "watermelon"];
    var fruitsObj = { …fruits };
    console.log(fruitsObj);
    // returns {0: "banana", 1: "apple", 2: "orange", 3: "watermelon", 4: "apple", 5: "orange", 6: "grape", 7: "apple"}
    
    

    6. 用数据填充数组

    var newArray = newArray(10).fill("1");
    console.log(newArray);
    // returns ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"]
    

    7. 合并数组

    var fruits = ["apple", "banana", "orange"];
    var meat = ["poultry", "beef", "fish"];
    var vegetables = ["potato", "tomato", "cucumber"];
    var food = […fruits, …meat, …vegetables];
    console.log(food);
    // ["apple", "banana", "orange", "poultry", "beef", "fish", "potato", "tomato", "cucumber"]
    

    8. 求两个数组的交集

    var numOne = [0, 2, 4, 6, 8, 8];
    var numTwo = [1, 2, 3, 4, 5, 6];
    var duplicatedValues = […newSet(numOne)].filter(item => numTwo.includes(item));
    console.log(duplicatedValues);
    // returns [2, 4, 6]
    
    

    9. 从数组中删除假值

    var mixedArr = [0, "blue", "", NaN, 9, true, undefined, "white", false];
    var trueArr = mixedArr.filter(Boolean);
    console.log(trueArr);
    // returns ["blue", 9, true, "white"]
    

    10. 从数组中获取随机值

    var colors = ["blue", "white", "green", "navy", "pink", "purple", "orange", "yellow", "black", "brown"];
    var randomColor = colors[(Math.floor(Math.random() * (colors.length  )))]
    

    11. 数组反转

    var colors = ["blue", "white", "green", "navy", "pink", "purple", "orange", "yellow", "black", "brown"];
    var reversedColors = colors.reverse();
    console.log(reversedColors);
    // returns ["brown", "black", "yellow", "orange", "purple", "pink", "navy", "green", "white", "blue"]
    

    12. .lastIndexOf()方法

    在Javascript中,有一个有趣的方法,它允许查找给定元素的最后一次出现的索引。例如,如果我们的数组有重复的值,我们可以找到它最后一次出现的位置。让我们看看代码示例:
    var nums = [1, 5, 2, 6, 3, 5, 2, 3, 6, 5, 2, 7];
    var lastIndex = nums.lastIndexOf(5);
    console.log(lastIndex);
    // returns 9
    

    13. 将数组中的所有值相加

    var nums = [1, 5, 2, 6];
    var sum = nums.reduce((x, y) => x + y);
    console.log(sum);
    // returns 14
    

    相关文章

      网友评论

          本文标题:工作中遇到的数组技巧

          本文链接:https://www.haomeiwen.com/subject/wxlgqrtx.html