JavaScript的数组反转 [英] Javascript's Array Reverse

查看:141
本文介绍了JavaScript的数组反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

究竟如何JavaScript的 array.reverse()工作?它通过和交换阵列中的每个元素?如果是这样,它需要O(n)的交换大小为n的数组?

我想我问的原因是因为我在想,如果 array.reverse()是一样的:

 为(VAR I = 0; I<则为a.length / 2;我++){
  变种支架=一个由[i];
  一个由[i] = A [则为a.length - 1 - 我]
  一个[则为a.length - 1 - i] =持有人;
}

注意:很抱歉,如果JavaScript的code我贴不正确,这是pretty下旬现在

修改:固定则为a.length 则为a.length / 2


解决方案

有关它是如何工作的全部细节,阅读规范的相关部分。这里的算法:


  

      
  1. 让Ø是调用ToObject传递这个值作为参数的结果。


      
      

        
    1. 让lenVal是调用的结果[[获取]以参数长度的O内部方法。

    2.   
    3. 让LEN是ToUint32(lenVal)。

    4.   
    5. 让中间是地板(LEN / 2)。

    6.   
    7. Letlower为0。

    8.   
    9. 重复,而较低的中间≠


        
        

          
      1. 让上进行LEN-低-1。

      2.   
      3. 让upperP是的ToString(上)。

      4.   
      5. 让lowerP是的ToString(下)。

      6.   
      7. 让lowerValue是调用的结果[[获取]]的O与参数lowerP内部方法。

      8.   
      9. 让upperValue是调用的结果[[获取]]与参数upperP的O内部方法。

      10.   
      11. 让lowerExists是调用的O的[[HasProperty]]内部方法与参数lowerP的结果。

      12.   
      13. 让upperExists是参数调用upperP的O的[[HasProperty]]内部方法的结果。

      14.   
      15. 如果lowerExists是真实的,upperExists是真的,那么


      16.   
      17. 调用[将]的O带有参数lowerP,upperValue,和真正的内部方法。


      18.   
      19. 调用[将]的O带有参数upperP,lowerValue,和真正的内部方法。

      20.   
      21. 否则,如果lowerExists是假的,upperExists为真,那么

      22.   
      23. 调用[将]的O带有参数lowerP,upperValue,和真正的内部方法。

      24.   
      25. 调用[删除]的O内部方法,带参数upperP和真实的。

      26.   
      27. 否则,如果lowerExists是真实的,upperExists是假的,那么

      28.   
      29. 调用[删除]的O内部方法,带参数lowerP和真实的。

      30.   
      31. 调用[将]的O带有参数upperP,lowerValue,和真正的内部方法。

      32.   
      33. 否则,无论lowerExists和upperExists都是假

      34.   
      35. 无需任何操作。

      36.   
      37. 增加1低。

      38.   

    10.   
    11. 返回O操作。

    12.   

  2.   

How exactly does Javascript's array.reverse() work? Does it go through and swap every element of the array? If so, does it take O(n) to swap an array of size n?

I guess the reason I am asking is because I was wondering if array.reverse() was the same as:

for(var i = 0; i < a.length / 2; i++) {
  var holder = a[i];
  a[i] = a[a.length - 1 - i];
  a[a.length - 1 - i] = holder;
}

NOTE: Sorry if the Javascript code I posted is incorrect, it's pretty late right now.

EDIT: Fixed a.length to a.length / 2.

解决方案

For the full details of how it works, read the relevant section of the spec. Here's the algorithm:

  1. Let O be the result of calling ToObject passing the this value as the argument.

    1. Let lenVal be the result of calling the [[Get]] internal method of O with argument "length".
    2. Let len be ToUint32(lenVal).
    3. Let middle be floor(len/2).
    4. Letlower be 0.
    5. Repeat, while lower ≠ middle

      1. Let upper be len−lower −1.
      2. Let upperP be ToString(upper).
      3. Let lowerP be ToString(lower).
      4. Let lowerValue be the result of calling the [[Get]] internal method of O with argument lowerP.
      5. Let upperValue be the result of calling the [[Get]] internal method of O with argument upperP .
      6. Let lowerExists be the result of calling the [[HasProperty]] internal method of O with argument lowerP.
      7. Let upperExists be the result of calling the [[HasProperty]] internal method of O with argument upperP.
      8. If lowerExists is true and upperExists is true, then

      9. Call the [[Put]] internal method of O with arguments lowerP, upperValue, and true .

      10. Call the [[Put]] internal method of O with arguments upperP, lowerValue, and true .
      11. Else if lowerExists is false and upperExists is true, then
      12. Call the [[Put]] internal method of O with arguments lowerP, upperValue, and true .
      13. Call the [[Delete]] internal method of O, with arguments upperP and true.
      14. Else if lowerExists is true and upperExists is false, then
      15. Call the [[Delete]] internal method of O, with arguments lowerP and true .
      16. Call the [[Put]] internal method of O with arguments upperP, lowerValue, and true .
      17. Else, both lowerExists and upperExists are false
      18. No action is required.
      19. Increase lower by 1.

    6. Return O .

这篇关于JavaScript的数组反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆