unpack array javascript code example
Example 1: unpack list javascript
var yourArray = [1, 2, 3, "four"]
[a, b, c, d] = yourArray
...yourArray
consloe.log(yourArray)
consloe.log(...yourArray)
Example 2: unpack array into variables javascript
var yourArr = [1, 2, 3]
[one, , three] = yourArray
Example 3: Destructuring Assignment
const HIGH_TEMPERATURES = {
yesterday: 75,
today: 77,
tomorrow: 80
};
const {today, tomorrow} = HIGH_TEMPERATURES;
const today = HIGH_TEMPERATURES.today;
const tomorrow = HIGH_TEMPERATURES.tomorrow;
console.log(today);
console.log(tomorrow);
console.log(yesterday);
Example 4: destructure array javascript
var [first, second, ...rest] = ["Mercury", "Earth", ...planets, "Saturn"];
Example 5: destructuring assignment
Destructuring assignment is special syntax introduced in ES6,
for neatly assigning values taken directly from an object.
const user = { name: 'John Doe', age: 34 };
const { name, age } = user;
Example 6: js object destructuring
const { [propName]: identifier } = expression;