.github
api-server
client
config
curriculum
challenges
_meta
arabic
chinese
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
debugging
es6
compare-scopes-of-the-var-and-let-keywords.chinese.md
complete-a-promise-with-resolve-and-reject.chinese.md
create-a-javascript-promise.chinese.md
create-a-module-script.chinese.md
create-an-export-fallback-with-export-default.chinese.md
create-strings-using-template-literals.chinese.md
declare-a-read-only-variable-with-the-const-keyword.chinese.md
explore-differences-between-the-var-and-let-keywords.chinese.md
handle-a-fulfilled-promise-with-then.chinese.md
handle-a-rejected-promise-with-catch.chinese.md
import-a-default-export.chinese.md
mutate-an-array-declared-with-const.chinese.md
prevent-object-mutation.chinese.md
reuse-javascript-code-using-import.chinese.md
set-default-parameters-for-your-functions.chinese.md
use--to-import-everything-from-a-file.chinese.md
use-arrow-functions-to-write-concise-anonymous-functions.chinese.md
use-class-syntax-to-define-a-constructor-function.chinese.md
use-destructuring-assignment-to-assign-variables-from-arrays.chinese.md
use-destructuring-assignment-to-assign-variables-from-nested-objects.chinese.md
use-destructuring-assignment-to-assign-variables-from-objects.chinese.md
use-destructuring-assignment-to-extract-values-from-objects.chinese.md
use-destructuring-assignment-to-pass-an-object-as-a-functions-parameters.chinese.md
use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements.chinese.md
use-export-to-share-a-code-block.chinese.md
use-getters-and-setters-to-control-access-to-an-object.chinese.md
use-the-rest-parameter-with-function-parameters.chinese.md
use-the-spread-operator-to-evaluate-arrays-in-place.chinese.md
write-arrow-functions-with-parameters.chinese.md
write-concise-declarative-functions-with-es6.chinese.md
write-concise-object-literal-declarations-using-object-property-shorthand.chinese.md
write-concise-object-literal-declarations-using-simple-fields.chinese.md
functional-programming
intermediate-algorithm-scripting
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-information-security-and-quality-assurance
08-coding-interview-prep
09-certificates
english
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7b8a367417b2b2512b4c | Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements | 1 | 301218 | 使用解构赋值配合 rest 操作符来重新分配数组元素 |
Description
Array.prototype.slice()
相同:
const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b); // 1, 2
console.log(arr); // [3, 4, 5, 7]
变量a
与b
分别获取了数组的前两个元素的值。之后,因为rest
操作符的存在,arr
获取了原数组剩余的元素的值,并构成了一个新的数组。
rest
操作只能对数组列表最后的元素起作用。这意味着你不能使用rest
操作符来截取原数组中间元素的子数组。
Instructions
rest
操作符来进行一个Array.prototype.slice
相同的操作。使得arr
是原数组source
除开前两个元素的子数组。
Tests
tests:
- text: <code>arr</code>应该为<code>[3,4,5,6,7,8,9,10]</code>
testString: assert(arr.every((v, i) => v === i + 3) && arr.length === 8);
- text: 没有使用<code>Array.slice()</code>。
testString: getUserInput => assert(!getUserInput('index').match(/slice/g));
- text: 使用了解构赋值。
testString: assert(code.replace(/\s/g, '').match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i));
Challenge Seed
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
// change code below this line
const arr = list; // change this
// change code above this line
return arr;
}
const arr = removeFirstTwo(source);
console.log(arr); // should be [3,4,5,6,7,8,9,10]
console.log(source); // should be [1,2,3,4,5,6,7,8,9,10];
Solution
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
// change code below this line
const [, , ...arr] = list;
// change code above this line
return arr;
}
const arr = removeFirstTwo(source);