首页 > 其他分享 >es6 解构用法:对 object 和 array 解构

es6 解构用法:对 object 和 array 解构

时间:2024-08-26 14:36:55浏览次数:4  
标签:es6 console log 解构 let array type name

参考文档:https://book.douban.com/subject/26864806/ (英文版+中文版)


 解构目的:为了使代码更简单、简洁

解构对象:object 和 array
【注意】

  • 针对对象和数组,使用语法不同。
  • 一定要注意同样的符号出现在 = 左边和右边是不同的含义!!
  • 解构时必须初始化(let, var, const),否则会报语法错误。
  • object通过属性名访问对象,所以解构时用属性名作为新变量名;数组通过索引访问,所以解构时也只和索引位置有关。
  • 解构对象时:
    • 原有属性可设置别名,但不可设置默认值(设置了不报错也不启用默认值)
    • 嵌套解构只新建最内层{ }中的属性名
    • 冒号后面有{ } 是内层解构,冒号后面直接是变量名字则是别名
  • 解构数组时:
    • 设置的默认值只有指定位置的变量不存在或其值未定义时才起效
    • 嵌套解构时 = 左边 [ ] 中的变量都存在,但都是单个的值
    • 用扩展运算符... 可以把剩余的值都放到一个变量中,但这个变量在解构中必须是最后一个变量

一、 对象解构

1.1 基础用法

 = 左边用 var, let 或 const 声明,然后一个 { } 包裹要新建的局部变量; = 右边是原始的对象。

左边用 { } 括起来是解构对象的语法,可以简单理解为右边是对象,所以左边也要是对象。

        let node = {
            type: "Identifier",
            name: "foo"
        };
        
        //因为右边是对象,所以左边也要是对象,所以用{}括起来,里面的type、name是原始变量的属性
        let { type, name } = node; 
        console.log(type); // "Identifier"
        console.log(name); // "foo"

单独的属性变量已经被定义后,想修改为原始对象的初始值:

type and name are initialized with values when declared, and then two variables with the same names are initialized with different values.

圆括号()让代码块变成表达式,表达式的值是 = 右侧的值。对象解构重新赋值时一定要加圆括号(),数组解构重新赋值不用加圆括号()

        let node = {
            type: "Identifier",
            name: "foo"
        },
// change the values of variables after they’re defined, type = "Literal", name = 5; // assign different values using destructuring ({ type, name } = node); console.log(type); // "Identifier" console.log(name); // "foo"

1.2 解构设置默认值(原有属性不可设置默认值,只有新属性才变量会使用默认值,因为新属性变量时原始对象中不存在的属性

 如果 = 左边有原始对象没有的属性名,则默认赋 undefined ,设置了默认值则为默认值。

        let node = {
            type: "Identifier",
            name: "foo"
        };
        let { type, name, value } = node;//value不设置默认值就是 undefined 
        console.log(type); // "Identifier"
        console.log(name); // "foo"
        console.log(value); // undefined
    -- 给新属性赋值和设置默认值
        let node = {
            type: "Identifier",
            name: "foo"
        };
        let { type, name, value = true } = node;
        console.log(type); // "Identifier"
        console.log(name); // "foo"
        console.log(value); // true

【注意】原有属性可设置别名,但不可设置默认值。(设置了不会报错,但也不会使用默认值,而是使用从解构获取的对象中的值)

When you use a destructuring assignment statement and you specify a local variable with a property name that doesn’t exist on the object, that local variable is assigned a value of undefined.

You can optionally define a default value to use when a specified property doesn’t exist.

        let node = {
            type: "Identifier",
            name: "foo"
        };
        let { type, name } = node;
        console.log(type); // "Identifier"
        console.log(name); // "foo"
        let node = {
            type: "Identifier",
            name: "foo"
        };
        // 给原有属性设置默认值无效,因为其值不是 null 或 undefined
        let { type, name="new name" } = node;
        console.log(type); // "Identifier"
        console.log(name); // "foo"

1.3 给原对象中属性起别名

        let node = {
        type: "Identifier"
        };
        // {原属性:别名}
        let { type: localType, name: localName = "bar" } = node;
        console.log(localType); // "Identifier"
        console.log(localName); // "bar"

1.4 嵌套解构

        let node = {
            type: "Identifier",
            name: "foo",
            loc: {
                start: {
                    line: 1,
                    column: 1
                },
                end: {
                    line: 1,
                    column: 4
                }
            }
        };

针对上面这个嵌套的对象,可以使用嵌套解构,但要注意:最后新建的本地变量只有最内层解构,外侧的loc变量并不存在!!

 

冒号后面有{ } 是内层解构,冒号后面直接是变量名字则是别名。

 

        // 此时loc变量不存在,存在的是start变量(解构出的变量是最内层{})
        let { loc: { start }} = node;
        console.log(start.line); // 1
        console.log(start.column); // 1
        console.log(loc); // 报错 Uncaught ReferenceError: loc is not defined

嵌套解构使用别名:

        // 冒号后面有{}是内层解构,冒号后面直接是变量名字则是别名
        let { loc: { start: localStart }} = node;
        console.log(localStart.line); // 1
        console.log(localStart.column); // 1

 二、数组解构

2.1 基础用法

 = 左边用 var, let 或 const 声明,然后一个 [ ] 包裹要新建的局部变量; = 右边是原始数组。

左边用 [ ] 括起来是解构数组的语法,可以简单理解为右边是数组,所以左边也要是数组。

        let colors = [ "red", "green", "blue" ];
        // Those values are chosen because of their position in the array; 
        // the actual variable names could be anything. 
        let [ firstColor, secondColor ] = colors;
        console.log(firstColor); // "red"
        console.log(secondColor); // "green"
        // 只要第三个位置的元素值
        let colors = [ "red", "green", "blue" ];
        let [ , , thirdColor ] = colors;
        console.log(thirdColor); // "blue"

单独的变量已经被定义后,想修改为原始数组的初始值:

        // 通过解构获得原始数组的值(无需圆括号())
        let colors = [ "red", "green", "blue" ],
        firstColor = "black",
        secondColor = "purple";
        // The only difference is that firstColor and  secondColor have already been defined.
        [ firstColor, secondColor ] = colors; // 数组解构时无需圆括号,对象解构时需要圆括号
        console.log(firstColor); // "red"
        console.log(secondColor); // "green"

2.2 数组解构的妙用:交换变量值

        // swapping variables in ECMAScript 6
        let a = 1,
        b = 2;
        // [] 在等号左边是解构,[]在等号右边是数组
        // 一定要注意同样的符号出现在 = 左边和右边是不同的含义!!
        // 解构,第一位置的变量值等于右边数组中索引为0的值,即 已定义的变量a被赋值为[b,a]数组i=0的值,也就是b
        // 同理,变量b被重新赋值为i=1的数组值a
        [ a, b ] = [ b, a ];
        console.log(a); // 2
        console.log(b); // 1

2.3 解构时设置默认值

只有当指定位置的属性不存在或其值未定义时,才会使用默认值。

        let colors = [ "red" ];
        // Array destructuring assignment allows you to specify a default value for any position in the array, too.
// The default value is used when the property at the given position either doesn’t exist or has the value undefined . let [ firstColor, secondColor = "green" ] = colors; console.log(firstColor); // "red" console.log(secondColor); // "green"

2.4 嵌套解构

外层解构的新变量也存在,但只会按照索引位置取一个值而不是数组,如果想让最后一个变量是数组,需要使用扩展运算符...

Rest items use the ... syntax to assign the remaining items in an array to a particular variable.

        let colors = [ "red", [ "green", "lightgreen" ], "blue" ];
        // later
        let [ firstColor, [ secondColor ] ] = colors;
        console.log(firstColor); // "red"
        console.log(secondColor); // "green"

使用扩展运算符... 必须是最后一个变量,且后面不能有逗号

        let colors = [ "red", "green", "blue" ];
        let [ firstColor, ...restColors ] = colors;//restColors 是数组
        console.log(firstColor); // "red"
        console.log(restColors.length); // 2
        console.log(restColors[0]); // "green"
        console.log(restColors[1]); // "blue"

 三、混合解构

    let node = {
        type: "Identifier",
        name: "foo",
    loc: {
        start: {
            line: 1,
            column: 1
        },
        end: {
            line: 1,
            column: 4
        }
    },
    range: [0, 3]
    };
    
    let { 
        loc: { start },
        range: [ startIndex ]
    } = node;
    
    console.log(start.line); // 1
    console.log(start.column); // 1
    console.log(startIndex); // 0

我觉得这样太复杂了,不过书上写这样十分有效,因为不用遍历整个json结构了。。

This approach is particularly useful for pulling values out of JSON configuration structures without navigating the entire structure.

标签:es6,console,log,解构,let,array,type,name
From: https://www.cnblogs.com/sunshine233/p/18380272

相关文章

  • 使用ArraysList集合类实现新闻管理系统
    新闻管理系统,需求如下:        可以存储各类新闻标题(包含ID、名称、创建者)。        可以获取新闻标题的总数。        可以逐条打印每条新闻标题的名称。 News类:新闻类来存储新闻的相关信息,包括ID、名称和创建者。publicclassNews{publi......
  • ArrayList遍历, 元素查找
    1,ArrayList集合的遍历与数组类似,都可以使用foreach语句string[]str1={"a","b","c","d","e","f"};ArrayListList=newArrayList(str1);foreach(variteminList)......
  • ArrayList元素的删除(4种函数)
    1Clear()方法Clear()方法用来从ArrayList中移除所有元素,语法格式如下。string[]str1={"a","b","c"};ArrayListList=newArrayList(str1);List.Clear();2Remove()方法Remove()方法用来从ArrayList中移除特定对象的第一个......
  • ArrayList声明,Add(), Insert();
     ArrayList提供了3个构造器,通过这3个构造器可以有3种声明方式。(1)默认构造器,会以默认大小(16位)初始化内部数组。构造器格式如下。ArrayListList=newArrayList();//实例化一个ArrayList,命名为List;for(inti=0;i<10;i++)//添加10个元素......
  • CodeForces - 1353D Constructing the Array
    CodeForces-1353D这道题也可能比较简单,主要是要想到优先队列要怎么使用,这一点如果用递归会写不了但是因为对优先队列不太熟悉,只有被提示可以用优先队列才想到要怎么用,还是很重要的STL注意运算符的重构应该反着来写其他的思维很朴素,运算符的重构就是,先比较长度,优先用长度长......
  • node.js: mysql sequelize es6 ORM (updated)
     /***description:*product:WebStorm*projectvuetstest*File:dbconfig.js*ds:$*IDE:webstorm2023.1*OS:windows10*database:mysql8+sqlserver2019postgreSQL16*dev:node20+vue.js3.0+*@authorhp*@projectvuetstest*......
  • ES6解构赋值详解;全面掌握:JavaScript解构赋值的终极指南
    目录全面掌握:JavaScript解构赋值的终极指南一、数组解构赋值1、基本用法2、跳过元素3、剩余元素4、默认值二、对象解构赋值1、基本用法2、变量重命名3、默认值4、嵌套解构三、复杂的嵌套结构解构四、函数参数解构赋值1、对象解构作为函数参数2、带有默认值的函......
  • ArrayList动态扩容机制(长度可变原理)
    ArrayList底层是数组结构的,数组的默认长度为10。当数组添加满了后,会自动扩容为1.5倍。原理讲解:1.用空参构造函数创建ArrayList集合容器。测试代码:publicclassArrayListDemo{publicstaticvoidmain(String[]args){//创建ArrayList集合容器......
  • 关于Arrays.asList返回的List无法新增和删除?
    这个是在写项目的时候发现的,然后就分析了一下源码,得其内部原理复现代码示例:publicclassArraysAsList{publicstaticvoidmain(String[]args){Integer[]array={1,2,3,4,5};List<Integer>list=Arrays.asList(array);list.forEach......
  • 一文彻底了解ES6中的var、let、const基本用法以及暂时性死区和变量提升的区别
             ECMAScript变量是松散类型的,意思是变量可以用于保存任何类型的数据。每个变量只不过是一个用于保存任意值的命名占位符。有三个关键字可以声明变量:var、let、const。其中var在ECMAScript的所有版本中都可以使用,而 const和let只能在ECMAScript6及更......