Here is the way you get value from an object:

var obj = {
color: "blue"
}
console.log(obj.color); //blue

Destructuring Assignment:


Object

Destructuring Assignment is somehow different:

var {color} = {
color: "green"
}
console.log(color); //green

It tells to looking for color property, so I get "green".

If I have muli pros (color, name, position, state): And I want to get color, and position properties.

var {color, position} = {
color: "green",
name: "Great",
position: "Finland",
state: "Who knows"
}
console.log(color); //green
console.log(position); //Finland

Function

If I have a fn which return an object: And from the object, I just want name and state.

function getObj(){
return{
color: "green",
name: "Great",
position: "Finland",
state: "Who knows"
};
} var {name, state} = getObj();
console.log(name); //Great
console.log(state); //Who knows

From the example, if you want to name something else:

{name: who, state: where} 
function getObj(){
return{
color: "green",
name: "Great",
position: "Finland",
state: "Who knows"
};
} var {name: who, state: where} = getObj();
console.log(who); //Great
console.log(where); //Who knows

Array

If I have an array, from which I just want the first and the third element from the array:

var [first,,third] = ['first', 'second', 'third', 'forth'];
console.log(first); //first
console.log(third); //third

If I want to forEach of array: and get the firstName

var people = [
{
firstName: "Allen",
secondName: "Hook",
email: "allen@abc.com"
},
{
firstName: "Anton",
secondName: "Tee",
email: "tee@abc.com"
},
{
firstName: "Yui",
secondName: "Gegg",
email: "gegg@abc.com"
}
]; people.forEach(({firstName}) => console.log(firstName)); /*
Allen
Anton
Yui
* */

To make it clear:

people.foreach(function(person){
console.log(person.firstName);
}); //Destructuring
people.foreach(function( {firstName} ){
console.log(firstName);
}); //Destructuring + Allow
people.foreach( ( {firstName} ) => console.log(firstName); )

or:

var [, secondPerson] = people;
function logEmail({email}){
console.log(email);
}
logEmail(secondPerson); //tee@abc.com

[ES6] 08. Destructuring Assignment -- 1的更多相关文章

  1. [ES6] 09. Destructuring Assignment -- 2

    Read More: http://es6.ruanyifeng.com/#docs/destructuring Array “模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值: Ex ...

  2. 逆转序列的递归/尾递归(+destructuring assignment)实现(JavaScript + ES6)

    这里是用 JavaScript 做的逆转序列(数组/字符串)的递归/尾递归实现.另外还尝鲜用了一下 ES6 的destructuring assignment + spread operator 做了 ...

  3. ES6 Destructuring Assignment All In One

    ES6 Destructuring Assignment All In One ES6 & Destructuring Assignment Axios, vue https://develo ...

  4. 解构赋值 Destructuring Assignment

    解构赋值 Destructuring Assignment ES6中可以通过一定的模式将数组或对象中的值直接赋值给外部变量,称为解构 对象的解构赋值 // 在ES5中,当需要获取一个对象中的变量值的时 ...

  5. Destructuring Assignment in JS(解构assignment in js)

    Destructuring Assignment In JavaScript 更省事,代码显得也清楚. Arrays 传统的声明赋值: let johnDoe = ["John", ...

  6. destructuring assignment

    [destructuring assignment] The destructuring assignment syntax is a JavaScript expression that makes ...

  7. Object Destructuring Assignment vs Object.assign

    Object Destructuring Assignment vs Object.assign // const params = Object.assign({}, this.$route.par ...

  8. js destructuring assignment bug

    js destructuring assignment bug https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Op ...

  9. ES6新特性:利用解构赋值 (destructuring assignment), 简化代码

    本文的Demo的运行环境为nodeJS, 参考:让nodeJS支持ES6的词法----babel的安装和使用 : 解构赋值是一种表达式, 利用这种新语法, 可以直接从数组或者对象中快速提取值 赋值给不 ...

随机推荐

  1. 【Numpy】python机器学习包Numpy基础知识学习

    一.安装:在之前的博客中已经写过:http://www.cnblogs.com/puyangsky/p/4763234.html 二.python数组切片知识: python中序列类有list.str ...

  2. SQL查询中关键词的执行顺序

    写在前面:最近的工作主要是写SQL脚本,在编写过程中对SQL的执行和解析过程特别混乱不清,造成了想优化却无从下手.为此专门在网上找博文学习,并做了如下总结. 1.查询中常用到的关键词有: SELECT ...

  3. luogu P1353 [USACO08JAN]跑步Running

    题目描述 The cows are trying to become better athletes, so Bessie is running on a track for exactly N (1 ...

  4. Codeforces 804D Expected diameter of a tree(树形DP+期望)

    [题目链接] http://codeforces.com/contest/804/problem/D [题目大意] 给你一个森林,每次询问给出u,v, 从u所在连通块中随机选出一个点与v所在连通块中随 ...

  5. 【Floyd】POJ2139 -Six Degrees of Cowvin Bacon

    普通的Floyd了分分秒可以水过,结果在submit前删调试段落的时候把程序本体给删了吃了两个WA…… #include<iostream> #include<cstring> ...

  6. 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence 暴力

    A. Robot Sequence 题目连接: http://www.codeforces.com/contest/626/problem/A Description Calvin the robot ...

  7. MYSQL复习笔记6-字符集

    Date: 20100101 Auth: Jin 参考http://blog.sina.com.cn/s/blog_9707fac301016wxm.html 一.字符集介绍 计算机只处理二进制代码 ...

  8. HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  9. Dependent Parameters in Concurrent Program using Special Value Set

    Dependent Parameters in Oracle Applications Requirement: Say there is a concurrent program that lets ...

  10. Express极简实例

    假设已创建一个Express工程,否则请参考express工程环境准备 修改app.js var express = require('express'); var app = express(); ...