Array destructuring in JavaScript

前端 未结 5 633
情歌与酒
情歌与酒 2020-11-30 12:21

I have this code in my vue-js app:

methods: {
    onSubmit() {
      ApiService.post(\'auth/sign_in\', {
        email: this.email,
        password: this.pa         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 12:42

    Beside of the given destructuring assignments, you could take an object destructuring for an array if you like to take certain elements, like the 11th and 15th element of an array.

    In this case, you need to use the object property assignment pattern [YDKJS: ES6 & Beyond] with a new variable name, because you can not have variables as numbers.

    var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
        { 11: a, 15: b } = array;
    
    console.log(a, b);

提交回复
热议问题