Examples of the method of the object array javaScript Es6

 Personal experience

 Before we no contact Es6 way to do some algorithms that sort of thing is more trouble in the process of doing a lot of problems have to be considered, too much trouble, and the method Es6 precisely to help us in everyday use when able when the issue of simplifying and greatly reduce our daily amount of code, such as we usually need to do some more complicated algorithm, need to use a lot of methods and cycles to be resolved, if these problems using Es6 resolve would not have seemed so cumbersome, will greatly reduce the time and complexity, greatly enhance our speed code, a word of it, Es6 really is worth learning the front of a small partner to spend time to conduct research on it, exploratory! ! !

 

  1. Extended operator

 


  meaning

  Extended operator is three dots (...), an array can be converted into a sequence of parameters separated by commas

    

  2. The flat array

 


 

 Multi-dimensional array is converted to one-dimensional array has always been a troublesome issue in terms of the amount of code or code logic, it is more troublesome, Es6 added a flat array of flat () method can be converted into a multidimensional array themselves We want multidimensional arrays, two-dimensional array, one-dimensional array, the following code

   

 This is not to use the new method Es6 written, very much the amount of code, code logic is a bit complicated, the issue under consideration is a trouble to think about how to avoid global pollution, we look at the following written using Es6

  

  Flat Es6 new () method is the default one flat array, if there is space, then the original array, will skip space, if it wants the multilayer array can be written in Flat Flat () method parameters only as a digital, if it is 2, then two flat, so:

         

  There is a flat () method supports  Infinity, meaning no matter how many layers of all the array into a one-dimensional array

    

  3.Object.keys

 


 

  Es6 this new method is relatively easy to learn, we want to get the object before the key is worth, then we should use for ... in to be acquired, or some other methods, too many words on recycling It will increase the efficiency of time, after using Object.keys would not want to use a for loop like to get the keys of the value of the object, did not talk much on the code:

  首先来看使用简单的for...in进行获取对象中的key值方法

           

  然后我们在看Es6的Object.keys()方法

  

  我们可以看到明显的区别使用for...in方法获取的key值会一个一个的输出, 而使用Object.keys获取的会形成一个数组输出,并且两者的代码量也是不同的,同时时间复杂度也是不用的,for...in使用了循环,我们都知道在js中只要使用循环就会产生时间复杂度,会降低我们的代码运行效率,所以看到这篇文章的小伙伴以后还是使用Es6的方法吧,比较方便也比较好用

  4.Object.values


 

  看到了上面Es6求对象中的key值方法,肯定有点疑惑,求对象中key值都这么方便的那求对象中的value值呢?我们就不用像上面那样接着说一些前言了,直接上代码,还是为了方便阅读先上for...in的代码

  

  我们最终的到的结果就是 小明 男 18 接下来看Es6的方法

  

  同样的也是形成一个数组输出

     5.数组中的find()和findindex()方法


 

  我们经常性的会做一些查找的功能,那么这两个方法,但是唯一的缺点就是只能查找到第一个符合条件的数组元素,非常的可惜,下面我们来看一看代码是如何写的

  

  其中的value参数就是代表的数组中每一个元素,而index则代表的是下标,arr就是代表这个数组,刚才也说过了就是我们Es6的find方法有一个弊端就是所有满足条件的成员中只能返回第一个符合条件的数组元素,接下来让我们看一看findindex方法:

   

  findIndex方法和find方法极其的相似有木有,但是二者返回的却是截然不同,find方法返回的是数组成员,而findindex则与其恰恰相反返回的是第一个符合条件的数组成员的位置,也就是下标,如果所有成员都满足的话也只能返回第一个符合条件的数组成员位置,看上图所展示的就是这样,数组元素中大于9的就是10和15,其中10和15的下标分别为2和3,由于10是第一个满足条件的数组元素,所以findindex返回的下标是2


 讲了这么多,肯定有很多的小伙伴已经跃跃欲试的想要实际操作了吧,我们在学习的过程中千万不要总是看,我们一定要上手实际的去进行操作,否则的话我们只是凭着自己的第一印象去理解代码,我们并不知道自己所看到的是否会出现BUG,会报错,这一点是非常致命的,最后:祝所有看过我文章的小伙伴们前程似锦、前途无量

 

 

 

Guess you like

Origin www.cnblogs.com/xiaoxiaoxiongmao/p/12031280.html