简体   繁体   English

使用数组解构而不是切片

[英]Use array destructuring instead of slice

How do I use array destructuring instead of Array.slice()如何使用数组destructuring而不是Array.slice()

Are there any benefits instead of slicing it?有什么好处而不是切片?

transposed[idx] = [ 'Calendar Name', 'Standard' ]

acc.name = transposed[idx].slice(1)[0]; // => 'Standard'

You could destruct the array with [arg1,arg2]您可以使用[arg1,arg2]破坏数组

 const [a,b] = [ 'Calendar Name', 'Standard' ] console.log(a,b)

for yours给你的

   const [calName,standard] = transposed[idx]
const [first,second] = transposed[idx];
// first = 'Calendar Name'
// second = 'Standard'

Honestly, the way to do this is transposed[idx][1] , not array destructuring or slice if all you want is the value Standard , if you're just going to drop Calendar Name on the floor anyway and not use it老实说,这样做的方法是transposed[idx][1] ,而不是数组解构或切片,如果你想要的只是值Standard ,如果你只是要把Calendar Name放在地板上而不使用它

Answer after editing the question编辑问题后回答

 const arr = ["Calendar Name", "Standard"]; const [first, second] = arr; console.log(first, second);

Answer before editing the question.在编辑问题之前回答。

 const arr = [ ["Calendar Name", "Standard"], ["Valid From", 44197], ["Valid To", 44561], ["Use Holidays", "yes"], [ "Working Day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", ], [ "Start", 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, 0.3333333333333333, "-", "-", ], [ "End", 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, "-", "-", ], ]; const [[first, second]] = arr; console.log(first, second);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM