1.删除数组的重复项

第一种方式
var fruits = ['banana', 'apple', 'orange', 'apple', 'orange', 'grape', 'watermelon']

var uniqueFruits = new Set(fruits)
console.log(uniqueFruits); // Set {'banana', 'apple', 'orange', 'grape', 'watermelon'}

var uniqueFruits = Array.from(new Set(fruits))
console.log(uniqueFruits); // [ 'banana', 'apple', 'orange', 'grape', 'watermelon' ]

第二种方式
var fruits = ['banana', 'apple', 'orange', 'apple', 'orange', 'grape', 'watermelon']

var uniqueFruits2 = [...new Set(fruits)]
console.log(uniqueFruits2); // [ 'banana', 'apple', 'orange', 'grape', 'watermelon' ]

2.替换数组中的特定值

var usernames = ['蜗牛0', '蜗牛1', '蜗牛2', '蜗牛3', '蜗牛4', '蜗牛5']
usernames.splice(0, 2, '蜗牛替换位置0', '蜗牛替换位置1')
console.log(usernames) // [ '蜗牛替换位置0', '蜗牛替换位置1', '蜗牛2', '蜗牛3', '蜗牛4', '蜗牛5' ]

3.Array.from 达到 .map 的效果

var friends = [
{ name: '小智', age: 22 },
{ name: '张三', age: 23 },
{ name: '李四', age: 24 },
{ name: '王二', age: 25 },
{ name: '麻子', age: 26 },]
var friendsNames = Array.from(friends, ({ name }) => name)
console.log(friendsNames) // [ '小智', '张三', '李四', '王二', '麻子' ]

4.置空数组

var friends = [
{ name: '小智', age: 22 },
{ name: '张三', age: 23 },
{ name: '李四', age: 24 },
{ name: '王二', age: 25 },
{ name: '麻子', age: 26 },]
friends.length = 0
console.log(friends) // []

5.将数组转换为对象

var usernames = ['蜗牛0', '蜗牛1', '蜗牛2', '蜗牛3']
var usernamesObj = { ...usernames }
console.log(usernamesObj) // { '0': '蜗牛0', '1': '蜗牛1', '2': '蜗牛2', '3': '蜗牛3' }

6.用数据填充数组

var array = new Array(10).fill('1')
console.log(array) // [ '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' ]

7.数组合并

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

8.求两个数组的交集

var num0 = [0, 2, 4, 6, 8, 9]
var num1 = [0, 1, 3, 5, 8, 9]
var jiaoJi = [...new Set(num0)].filter(item => num1.includes(item))
console.log(jiaoJi) // [ 0, 8, 9 ]

9.从数组中删除虚值

var mixedArray = ['1', 0, false, null, '', NaN, 9, undefined, 'apple']
var trueArray = mixedArray.filter(Boolean)
console.log(trueArray) // [ '1', 9, 'apple' ]

10.从数组中获取随机值

var number = [0, 2, 4, 5, 7, 9, 22, 55]
var randomNumber = number[(Math.floor(Math.random() * (number.length)))]
console.log(randomNumber) // 9

11.反转数组

var number = [0, 2, 4, 5, 7, 9, 22, 55]
var numberReverse = number.reverse()
console.log(numberReverse) // [ 55, 22, 9, 7, 5, 4, 2, 0 ]

12.lastIndexOf() 方法

var number = [1, 5, 3, 8, 3, 9, 23, 65, 3, 99, 3, 4]
var lastNumber = number.lastIndexOf(3)
console.log(lastNumber) // 10

13.对数组中的所有值求和

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

JS数组精简的十三个技巧的更多相关文章

  1. 13 个 JS 数组精简技巧

    来自 https://juejin.im/post/5db62f1bf265da4d560906ab 侵删 数组是 JS 最常见的一种数据结构,咱们在开发中也经常用到,在这篇文章中,提供一些小技巧,帮 ...

  2. js 数组去重小技巧

    js 数组去重小技巧 Intro 今天遇到一个问题,需要对数据进行去重,想看一下有没有什么比较方便的方法,果然有些收获. Question 问题描述: 我有一个这样的数据: [ { "Pro ...

  3. 【转】天啦噜!原来Chrome自带的开发者工具还能这么用!(提升JS调试能力的10个技巧)

    天啦噜!原来Chrome自带的开发者工具还能这么用! (提升JS调试能力的10个技巧)   Chrome自带开发者工具.它的功能十分丰富,包括元素.网络.安全等等.今天我们主要介绍JavaScript ...

  4. 优化 JS 条件语句的 5 个技巧

    优化 JS 条件语句的 5 个技巧 原创: 前端大全 前端大全 昨天 (给前端大全加星标,提升前端技能) 编译:伯乐在线/Mr.Dcheng http://blog.jobbole.com/11467 ...

  5. js数组方法大全(下)

    # js数组方法大全(下) 记录一下整理的js数组方法,免得每次要找方法都找不到.图片有点多,注意流量,嘻嘻! 本期分享 forEach() map() filer() every() some() ...

  6. 处理 JS中 undefined 的 7 个技巧

    摘要: JS的大部分报错都是undefined... 作者:前端小智 原文:处理 JS中 undefined 的 7 个技巧 Fundebug经授权转载,版权归原作者所有. 大约8年前,当原作者开始学 ...

  7. 小兔JS教程(四)-- 彻底攻略JS数组

    在开始本章之前,先给出上一节的答案,参考答案地址: http://www.xiaotublog.com/demo.html?path=homework/03/index2 1.JS数组的三大特性 在J ...

  8. js数组学习整理

    原文地址:js数组学习整理 常用的js数组操作方法及原理 1.声明数组的方式 var colors = new Array();//空的数组 var colors = new Array(3); // ...

  9. 转→js数组遍历 千万不要使用for...in...

    看到一篇内容还不错,但是排版实在糟糕, 逼死强迫症患者啊,直接拉下去找原文连接,找到了,但是已经消失了···500错误... 第一次因为实在看不下去一篇博客的排版, 为了排版而转载... 转载地址:h ...

随机推荐

  1. 安装ubuntu18.04.3全过程

    目录 一.安装ubuntu18.04.3操作系统 二.系统设置 三.非开发常用软件安装 四.开发常用软件安装 五.ubuntu相关知识 六.参考文章链接 正文 一.安装ubuntu18.04.3操作系 ...

  2. 第六节:前后端交互之axios用法及async异步编程

    一. axios用法 参考: API文档: https://www.kancloud.cn/yunye/axios/234845 GitHub: https://github.com/axios/ax ...

  3. windows ,linux永久和临时修改pip源

    临时修改(建议)pypi镜像源方法:如果有untrust 报错,可使用https开头的网站,或加上--trusted 例如: pip install pywin32 -i http://mirrors ...

  4. Java的单例模式(singleton)

    为什么需要单例?只因为国家的独生子女政策(当然现在可以生2个) 单例是一个很孤独的物种,因为它的类里面做多只有也仅只有它一个. 常见的是懒汉及饿汉模式, 1.懒汉,为什么这么叫,看看英文,原为lazy ...

  5. 动态规划: 最大m子段和问题的详细解题思路(JAVA实现)

    这道最大m子段问题我是在课本<计算机算法分析与设计>上看到,课本也给出了相应的算法,也有解这题的算法的逻辑.但是,看完之后,我知道这样做可以解出正确答案,但是我如何能想到要这样做呢? 课本 ...

  6. js中substr、substring、indexOf、lastIndexOf的用法

    substr substr(start,length)表示从start位置开始,截取length长度的字符串 var str="imgs/header_2.jpg"; consol ...

  7. Unity中调用Windows窗口选择文件

    1.OpenFileName数据接收类,如下: using UnityEngine; using System.Collections; using System; using System.Runt ...

  8. WLC HA模式下的注意事项

    管理控制器:1.控制器默认开启的是SSH (CLI),Secure Web/https (GUI)2.登录控制器的管理地址为Active设备所控制(主备的配置同步,所以管理地址一致)3.WLC HA状 ...

  9. Mac终端ls颜色设置

    mac自带的终端是款非常好用的ssh工具,但ls命令下文件与文件夹都是单一的颜色,为了更好区分,作出修改. 终端默认背景颜色为白色,(终端->偏好设置->描述文本),可修改背景颜色与字体大 ...

  10. Nginx常用模块及作用

    Nginx模块详解 nginx模块分为两种,官方和第三方,我们通过命令 nginx -V 查看 nginx已经安装的模块! [root@localhost ~]# nginx -V nginx ver ...