javascript ES6 - 解构阵列

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ES6 - 解构阵列相关的知识,希望对你有一定的参考价值。

const details = ['Arden', 123, 'arden.nl'];
// get a bunch of values out of the details array and name them as variable
const [name, id, website] = details;
console.log(name, id, website);

const data = 'Basketball,Sports,90210,23,super,man,cool';
// We use data.split(',') which takes in the data string and returns it as an
// array which we immediately destructure with es6 array destructuring. 

// When we have values that we do not call while destructuring, nothing will
// happen with them.
const [itemName, category, sku, inventory] = data.split(',');

const team = ['Cap', 'Hulk', 'Black Widow', 'Iron Man', 'Thor'];
// Here we destructure the first and second values from the team array and return 
// them in variables called `captain` and `assistant`. We use the `rest` operator (`...`) to
// spread out the remaining values in an array called `players`
const [captain, assistant, ...players] = team;

以上是关于javascript ES6 - 解构阵列的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript ES6 - 解构赋值

JavaScript ES6 - 解构赋值

JavaScript ES6 - 解构赋值

javascript 解构#es6

javascript ES6 - 解构函数

javascript ES6解构