Files
.github
api-server
client
config
curriculum
docs
guide
arabic
chinese
english
3d
accessibility
agile
algorithms
android-development
angular
angularjs
apache
aspnet
bash
blockchain
book-recommendations
bootstrap
bsd-os
bulma
c
canvas
certifications
apis-and-microservices
coding-interview-prep
data-visualization
front-end-libraries
information-security-and-quality-assurance
javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
-iterate-through-the-keys-of-an-object-with-a-for...in-statement
access-an-arrays-contents-using-bracket-notation
access-property-names-with-bracket-notation
add-items-to-an-array-with-push-and-unshift
add-items-using-splice
add-key-value-pairs-to-javascript-objects
check-for-the-presence-of-an-element-with-indexof
check-if-an-object-has-a-property
combine-arrays-with-the-spread-operator
copy-an-array-with-the-spread-operator
index.md
copy-array-items-using-slice
create-complex-multi-dimensional-arrays
generate-an-array-of-all-object-keys-with-object.keys
iterate-through-all-an-arrays-items-using-for-loops
modify-an-array-stored-in-an-object
modify-an-object-nested-within-an-object
remove-items-from-an-array-with-pop-and-shift
remove-items-using-splice
use-an-array-to-store-a-collection-of-data
use-the-delete-keyword-to-remove-object-properties
index.md
basic-javascript
debugging
es6
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
index.md
responsive-web-design
index.md
chef
clojure
cloud-development
computational-genomics
computer-hardware
computer-science
containers
cplusplus
csharp
css
d3
data-science-tools
design-patterns
designer-tools
developer-ethics
developer-tools
devops
docker
documentation
drupal
electron
elixir
elm
erlang
fsharp
game-development
gatsbyjs
git
go
groovy
haskell
hibernate
html
ionic
java
javascript
joomla
jquery
julia
kotlin
laravel
linux
logic
machine-learning
mathematics
meta
miscellaneous
mobile-app-development
mongodb
natural-language-processing
neovim
network-engineering
nginx
nodejs
php
product-design
progressive-web-apps
puppet
python
r
react
react-native
redux
rest-api
robotics
rt-os
ruby
rust
sass
security
semantic-ui
software-engineering
sql
ssh
svg
svn
swift
terminal-commandline
tomcat
tools
typescript
typography
user-experience-design
user-experience-research
vagrant
vim
virtualbox
visual-design
voice
vue
vue-cli
web-augmented-reality
web-components
web-performance
web-virtual-reality
wordpress
working-in-tech
xml
portuguese
russian
spanish
mock-guide
news
tools
.editorconfig
.eslintignore
.eslintrc
.gitattributes
.gitignore
.node-inspectorrc
.prettierrc
.snyk
.travis.yml
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE.md
README(french).md
README.md
docker-compose-shared.yml
docker-compose.yml
lerna.json
netlify.toml
package-lock.json
package.json
sample.env

35 lines
977 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Copy an Array with the Spread Operator
---
## Copy an Array with the Spread Operator
- The final hint in the example tells you to use a recently learned method.
- The spread operator copies all elements into a new empty object.
```javascript
while (num >= 1) {
newArr = [...arr]
num--;
}
```
- The code above will copy all of the elements into `newArr` but will also reinitialise `newArr` with every new iteration of the while loop.
- A new variable should first be initialised using the spread operator - `let obj = [...arr];` - then this variable should be added to the `newArr` for every iteration of the while loop.
## Solution:
```javascript
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
// change code below this line
newArr.push([...arr]);
// change code above this line
num--;
}
return newArr;
}
// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));
```