- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using Array push() Method and Spread Syntax
The spread operator can be used in this scenario to extend the items from an array with another array.
array1.push( ...array2 )
In the above syntax, we are extending the array1 with array2 values.
Step 1 − Create an array.
Step 2 − Create another array that might be added to the previous array.
Step 3 − Use the push() method with the first array.
Step 4 − Mention another array in the brackets.
In this example, we have created two arrays and used the push method to extend the numbers listed in array number 2 in array number 1. You can also use multiple arrays and provide conditions using the “if and else” method.
<html> <body> <div id = "my"> </div> <script> const number1 = [1, 2, 3] const number2 = [4, 5, 6] const text = "New Array is: " document.getElementById("my").innerHTML = text + number1; </script> </body> </html>
Using Array push.apply() Method
While the push method is commonly used, apply method can also provide similar output. This method will likely consider the mentioned array as the first argument and extend other arguments using the push method.
Step 1 − Make a new array of numbers or words.
Step 2 − Create another array that will be used to extend.
Step 3 − Use the apply method with the push one using the array that needs to be extended with another one.
Step 4 − Mention the list of arrays that needs to be added following the correct order.
The output of the array is similar to the one we got previously using the Push method. You can also use the prototype with the push and apply the method to get the same result.
Using Array concat() Method
Here we will write four arrays and use the concat() method twice to see how the arrays get extended with another array.
Using Spread Operator to Extend an Array
Compared to other methods, the spread operator is easy to remember and makes the code look clear.
array1 = [...array1, ...array2]
Here we have used two let variables because the const variable can’t be used for the spread operator method. After running this script, you will find that array “number2” repeats the output of the spread method of array “number1” and the numbers of array “number2”.
The reason is that the array number1 is first assigned with the method, and when it gets used in the second method, it repeats the output.
Realizing the desired output before writing the code is the first step. You can get lasting results and use these methods for various websites.
The methods like spread operators and concat() can be used to experiment with arrays and get different outputs. Although these methods look straightforward, you can only make the best use of them through trial and error.
- How to extend the size of an array in Java
- How to extend an array after initialization in android listview?
- Empty masked array with the properties of an existing array in Numpy
- Java Program to extend the size of an Integer array
- How to wrap an existing element with another one in jQuery?