JavaScript 如何将arguments对象转换为数组

JavaScript 如何将arguments对象转换为数组

arguments对象是一个类似数组的对象,表示在调用函数时传递的参数。这个类似数组的对象没有数组的原型链,因此无法使用任何数组的方法。可以使用两种方法将这个对象转换为一个真正的数组:

方法1:使用Array.from()方法

Array.from()方法用于创建一个新数组,该数组是一个类似数组或可迭代对象的浅拷贝。它包含三个参数,其中第一个参数是要转换的类似数组的对象,第二个参数是可选的映射函数,可以对数组的每个元素调用该函数,第三个参数是可用于映射函数的“this”参数。将arguments对象作为参数传递给这个方法,它将返回对象的数组形式。这个数组有数组的原型链,可以与各种数组方法一起使用。

语法:

argumentsArray = Array.from(arguments)

示例:

<!DOCTYPE html>
<html>
     
<head>
    <title>
        How to convert arguments object into
        an array in JavaScript?
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b> 
        How to convert arguments object into
        an array in JavaScript?
    </b>
    <p>
        Click on the button to convert the
        arguments object to an Array
        and also use the Array join() method.
    </p>
    <p>The arguments passed are '1, "Two", 3'</p>
    <button onclick="convertToArray(1, 'Two', 3)">
        Convert to Array
    </button>
     
    <script type="text/javascript">
     
        function convertToArray(a, b, c) {
            console.log("The arguments object:")
            console.log(arguments);
         
            argumentsArray = Array.from(arguments);
         
            console.log("The arguments object as array:")
            console.log(argumentsArray);
         
            // Example of using the join() method
            joinedArray = argumentsArray.join(', ');
             
            console.log("Using join() on array: ",
                                    joinedArray);
        }
    </script>
</body>
 
</html>

输出:

  • 显示:

JavaScript 如何将arguments对象转换为数组

  • 控制台:

JavaScript 如何将arguments对象转换为数组

方法2:使用Array.prototype.slice()方法

Array.prototype.slice()方法用于将数组的一部分返回到一个新的数组对象中。这个新数组是原始数组的浅拷贝。这个方法有两个可选参数,用来指定数组的起始索引和结束索引。如果省略了这些参数,索引会设置为数组的第一个索引和最后一个索引。这个方法可以通过将该方法绑定到对象上来转换arguments对象。绑定是通过使用call()函数并将类似数组的对象作为参数传递给它来完成的。它将返回一个arguments对象的数组形式。这个数组有数组的原型链,并且可以与各种数组方法一起使用。

语法:

argumentsArray = Array.prototype.slice.call(arguments)

示例:

<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to convert arguments object
        into an array in JavaScript?
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to convert arguments object into
        an array in JavaScript?
    </b>
    <p>
        Click on the button to convert the
        arguments object to an Array and
        also use the Array join() method.
    </p>
    <p>The arguments passed are '1, "Two", 3'</p>
    <button onclick="convertToArray(1, 'Two', 3)">
        Convert to Array
    </button>
     
    <script type="text/javascript">
     
        function convertToArray(a, b, c) {
            console.log("The arguments object:")
            console.log(arguments);
         
            argumentsArray = 
                Array.prototype.slice.call(arguments);
         
            console.log("The arguments object as array:")
            console.log(argumentsArray);
         
            // Example of using the join() method
            joinedArray = 
                argumentsArray.join(', ');
                 
            console.log("Using join() on array: ",
                                       joinedArray);
        }
    </script>
</body>
 
</html>

输出:

  • 显示:

JavaScript 如何将arguments对象转换为数组

  • 控制台:

JavaScript 如何将arguments对象转换为数组

方法3:使用扩展运算符

扩展运算符允许在预期为0个或多个参数的位置上扩展可迭代对象。该运算符会在原地扩展参数,并使用方括号捕获并组成数组。我们将在原地扩展参数对象并形成一个数组。

语法:

var Array_obj = [...arguments];

示例:

<!DOCTYPE html>
<html>
     
<head>
    <title>
        How to convert arguments object into
        an array in JavaScript?
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to convert arguments object into
        an array in JavaScript?
    </b>
    <p>
        Click on the button to convert the
        arguments object to an Array
        and also use the Array join() method.
    </p>
       <p>The arguments passed are '1, "Two", 3'</p>
    <button onclick="convertToArray(1, 'Two', 3)">
        Convert to Array
    </button>
     
    <script type="text/javascript">
     
        function convertToArray(a, b, c) {
            console.log("The arguments object:")
            console.log(arguments);
         
            argumentsArray = [...argument];
         
            console.log("The arguments object as array:")
            console.log(argumentsArray);
         
            // Example of using the join() method
            joinedArray = argumentsArray.join(', ');
             
            console.log("Using join() on array: ", joinedArray);
        }
    </script>
</body>
 
</html>

输出:

JavaScript 如何将arguments对象转换为数组

  • 控制台显示:

JavaScript 如何将arguments对象转换为数组

方法4:使用Object.values()方法

Object.values方法用于创建一个数组,其值来自对象的值。

语法:

var Array_obj = Object.values( obj );

示例:

<!DOCTYPE html>
<html>
     
<head>
    <title>
        How to convert arguments object into
        an array in JavaScript?
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to convert arguments object into
        an array in JavaScript?
    </b>
    <p>
        Click on the button to convert the
        arguments object to an Array
        and also use the Array join() method.
    </p>
    <p>The arguments passed are '1, "Two", 3'</p>
     
    <button onclick="convertToArray(1, 'Two', 3)">
        Convert to Array
    </button>
     
    <script type="text/javascript">
     
        function convertToArray(a, b, c) {
            console.log("The arguments object:")
            console.log(arguments);
            argumentsArray = Object.values(arguments);
         
            console.log("The arguments object as array:")
            console.log(argumentsArray);
         
            // Example of using the join() method
            joinedArray = argumentsArray.join(', ');
             
            console.log("Using join() on array: ",
                                    joinedArray);
        }
    </script>
</body>
 
</html>

输出:

JavaScript 如何将arguments对象转换为数组

  • 控制台输出:

JavaScript 如何将arguments对象转换为数组

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程