Array.prototype.slice()
, как показано ниже: const [a, b, ... arr] = [1, 2, 3, 4, 5, 7];Переменные
console.log (a, b); // 1, 2
console.log (обр); // [3, 4, 5, 7]
a
и b
принимают первое и второе значения из массива. После этого из-за присутствия оператора отдыха arr
получает остальные значения в виде массива. Элемент rest работает корректно только как последняя переменная в списке. Как и в случае, вы не можете использовать оператор rest, чтобы поймать субарей, который не содержит последний элемент исходного массива. arr
should be [3,4,5,6,7,8,9,10]
");'
- text: ''
testString: 'getUserInput => assert(getUserInput("index").match(/\[\s*\w*\s*,\s*\w*\s*,\s*...\w+\s*\]/g),"Destructuring should be used.");'
- text: ''
testString: 'getUserInput => assert(!getUserInput("index").match(/slice/g), "Array.slice()
should not be used.");'
```