* Adding proper solution for spread operation Use the Spread Operator to Evaluate Arrays In-Place: adding the proper solution for the challenge * made some more changes * Fixed the Solution in es6 class constructor guide Fixed the vegetable solution in Guide: Use class Syntax to Define a Constructor Function
1.8 KiB
1.8 KiB
title
title |
---|
Use the Spread Operator to Evaluate Arrays In-Place |
Use the Spread Operator to Evaluate Arrays In-Place
Spread Operator explained
Mozilla Developer Network Spread Operator
Spread Operator compared to Rest Parameter
Video Explaining Spread Operator and Rest Parameter
Information About apply() Method
Mozilla Developer Network Apply Method
3 Quick Examples
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min.apply(null, numbers);
console.log(minNum);//-12
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min(numbers);
console.log(minNum);//NaN
let numbers = [-12, 160, 0, -3, 51];
let minNum = Math.min(...numbers);
console.log(minNum);//-12
SPOILER WARNING: SOLUTION AHEAD
The Solution
Unpacking the arr1 using the spread operator and then copying those values to arr2
const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
"use strict";
arr2 = [...arr1]; // change this line
})();
console.log(arr2);