[ Web Proxy ]
URL:
Viewing: https://zh.javascript.info/destructuring-assignment [Back]  [Original]

:
Light themeDark theme
DanskEnglishEspaolFranaisIndonesiaItalianoTrkeOzbek

JavaScript Object Array

/

// 
let arr = ["John", "Smith"]

// 
//  firstName = arr[0]
//  surname = arr[1]
let [firstName, surname] = arr;

alert(firstName); // John
alert(surname);  // Smith

split

let [firstName, surname] = "John Smith".split(' ');
alert(firstName); // John
alert(surname);  // Smith

// let [firstName, surname] = arr;
let firstName = arr[0];
let surname = arr[1];

// 
let [firstName, , title] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];

alert( title ); // Consul

title

let [a, b, c] = "abc"; // ["a", "b", "c"]
let [one, two, three] = new Set([1, 2, 3]);

= for..of

let user = {};
[user.name, user.surname] = "John Smith".split(' ');

alert(user.name); // John
alert(user.surname); // Smith
.entries()

Object.entries(obj)

.entries()

let user = {
  name: "John",
  age: 30
};

// 
for (let [key, value] of Object.entries(user)) {
  alert(`${key}:${value}`); // name:John, then age:30
}

Map Map

let user = new Map();
user.set("name", "John");
user.set("age", "30");

// Map  [key, value] 
for (let [key, value] of user) {
  alert(`${key}:${value}`); // name:John, then age:30
}

let guest = "Jane";
let admin = "Pete";

//  guest = Peteadmin = Jane
[guest, admin] = [admin, guest];

alert(`${guest} ${admin}`); // Pete Jane

let [name1, name2] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];

alert(name1); // Julius
alert(name2); // Caesar
// 

"..."

let [name1, name2, ...rest] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];

// rest 
alert(rest[0]); // Consul
alert(rest[1]); // of the Roman Republic
alert(rest.length); // 2

rest

rest

let [name1, name2, ...titles] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
//  titles = ["Consul", "of the Roman Republic"]

undefined

let [firstName, surname] = [];

alert(firstName); // undefined
alert(surname); // undefined

=

// 
let [name = "Guest", surname = "Anonymous"] = ["Julius"];

alert(name);    // Julius
alert(surname); // Anonymous

prompt

// 
let [name = prompt('name?'), surname = prompt('surname?')] = ["Julius"];

alert(name);    // Julius
alert(surname); // 

prompt surname

let {var1, var2} = {var1:, var2:}

pattern {...}

let options = {
  title: "Menu",
  width: 100,
  height: 200
};

let {title, width, height} = options;

alert(title);  // Menu
alert(width);  // 100
alert(height); // 200

options.titleoptions.width options.height

//  let {...} 
let {height, width, title} = { title: "Menu", height: 200, width: 100 }

pattern

options.width w

let options = {
  title: "Menu",
  width: 100,
  height: 200
};

// { sourceProperty: targetVariable }
let {width: w, height: h, title} = options;

// width -> w
// height -> h
// title -> title

alert(title);  // Menu
alert(w);      // 100
alert(h);      // 200

width w height h title

"="

let options = {
  title: "Menu"
};

let {width = 100, height = 200, title} = options;

alert(title);  // Menu
alert(width);  // 100
alert(height); // 200

/

prompt width title

let options = {
  title: "Menu"
};

let {width = prompt("width?"), title = prompt("title?")} = options;

alert(title);  // Menu
alert(width);  // (prompt )

let options = {
  title: "Menu"
};

let {width: w = 100, height: h = 200, title} = options;

alert(title);  // Menu
alert(w);      // 100
alert(h);      // 200

let options = {
  title: "Menu",
  width: 100,
  height: 200
};

//  title 
let { title } = options;

alert(title); // Menu

pattern

pattern IE Babel polyfill

let options = {
  title: "Menu",
  height: 200,
  width: 100
};

// title =  title 
// rest = 
let {title, ...rest} = options;

//  title="Menu", rest={height: 200, width: 100}
alert(rest.height);  // 200
alert(rest.width);   // 100
let

let {} = {} let

let title, width, height;

// 
{title, width, height} = {title: "Menu", width: 200, height: 100};

JavaScript {...}

{
  // 
  let message = "Hello";
  // ...
  alert( message );
}

JavaScript

JavaScript (...)

let title, width, height;

// 
({title, width, height} = {title: "Menu", width: 200, height: 100});

alert( title ); // Menu

pattern

options size items pattern

let options = {
  size: {
    width: 100,
    height: 200
  },
  items: ["Cake", "Donut"],
  extra: true
};

// 
let {
  size: { //  size 
    width,
    height
  },
  items: [item1, item2], //  items 
  title = "Menu" // 
} = options;

alert(title);  // Menu
alert(width);  // 100
alert(height); // 200
alert(item1);  // Cake
alert(item2);  // Donut

options extra

[]

widthheightitem1item2 title

size items

function showMenu(title = "Untitled", width = 200, height = 100, items = []) {
  // ...
}

IDE undefined

//  undefined
showMenu("My Menu", undefined, undefined, ["Item1", "Item2"])

// 
let options = {
  title: "My menu",
  items: ["Item1", "Item2"]
};

// 
function showMenu({title = "Untitled", width = 200, height = 100, items = []}) {
  // title, items   options
  // width, height  
  alert( `${title} ${width} ${height}` ); // My Menu 200 100
  alert( items ); // Item1, Item2
}

showMenu(options);

let options = {
  title: "My menu",
  items: ["Item1", "Item2"]
};

function showMenu({
  title = "Untitled",
  width: w = 100,  // width goes to w
  height: h = 200, // height goes to h
  items: [item1, item2] // items first element goes to item1, second to item2
}) {
  alert( `${title} ${w} ${h}` ); // My Menu 100 200
  alert( item1 ); // Item1
  alert( item2 ); // Item2
}

showMenu(options);

function({
  incomingProperty: varName = defaultValue
  ...
})

incomingProperty varName defaultValue

showMenu()

showMenu({}); // 

showMenu(); // 

{}

function showMenu({ title = "Menu", width = 100, height = 200 } = {}) {
  alert( `${title} ${width} ${height}` );
}

showMenu(); // Menu 100 200

{}

  • let {prop : varName = default, ...rest} = object

    prop varName default

    rest

  • let [item1 = default, item2, ...rest] = array

    item1 item2 rest

  • /

: 5

let user = {
  name: "John",
  years: 30
};

  • name name
  • years age
  • isAdmin isAdmin false

let user = { name: "John", years: 30 };

// 
// ... = user

alert( name ); // John
alert( age ); // 30
alert( isAdmin ); // false
let user = {
  name: "John",
  years: 30
};

let {name, years: age, isAdmin = false} = user;

alert( name ); // John
alert( age ); // 30
alert( isAdmin ); // false

: 5

salaries

let salaries = {
  "John": 100,
  "Pete": 300,
  "Mary": 250
};

topSalary(salaries)

  • salaries null

P.S. Object.entries /

function topSalary(salaries) {

  let maxSalary = 0;
  let maxName = null;

  for(let [name, salary] of Object.entries(salaries)) {
    if (maxSalary < salary) {
      maxSalary = salary;
      maxName = name;
    }
  }

  return maxName;
}


Web Proxy Viewer  |  New URL  |  Original Page