简体   繁体   English

解构复杂对象

[英]Destructuring Complex Object

Thanks so much for all the help with this answer you guys!!非常感谢你们对这个答案的所有帮助!! I'm very new to coding in general and you are making it a lot easier to get a grip on things!总的来说,我对编码非常陌生,您可以更轻松地掌握事物! So far, I've got updated code kind of based on what I saw here (I didn't want to just copy & paste the answer, I wanted to learn everything that was happening with the code).到目前为止,我已经根据我在这里看到的内容更新了代码(我不想只是复制和粘贴答案,我想了解代码中发生的一切)。 From what I have now, the only thing I'm having trouble with is removing the "undefined" result from the output.从我现在所拥有的,我唯一遇到的问题是从输出中删除“未定义”结果。 I'm assuming this has something to do with an "if" statement inside the "for" loop, but I wasn't sure.我假设这与“for”循环中的“if”语句有关,但我不确定。 Any insights?任何见解?

 let userList = { "people": [ { firstName: "Fred", lastName: "Smith", dateOfBirth: 1980, spokenLanguages: { native: "English", fluent: "Spanish", intermediate: "Chinese" } }, { firstName: "Monica", lastName: "Taylor", dateOfBirth: 1975, spokenLanguages: { native: "Spanish", fluent: "English", intermediate: "French" } }, { firstName: "Maurice", lastName: "Edelson", dateOfBirth: 1992, spokenLanguages: { native: "English", fluent: "Spanish", } }, { firstName: "Kelly", lastName: "Lang", dateOfBirth: 1982, spokenLanguages: { native: "English", fluent: "German", intermediate: "Dutch" } } ] }; for (var i = 0; i < userList.people.length; i++) { let table = document.getElementById("info"); let newRow = table.insertRow(1); let cell = newRow.insertCell(0); let dobCell = newRow.insertCell(1); let langs = newRow.insertCell(2); let { firstName } = userList.people[i]; let { lastName } = userList.people[i]; let { dateOfBirth } = userList.people[i]; let { spokenLanguages: { native, fluent, intermediate } } = userList.people[i]; cell.innerHTML += firstName + ' ' + lastName + "<br/>"; dobCell.innerHTML += dateOfBirth + "<br/>"; langs.innerHTML += native + ', ' + fluent + ', ' + intermediate + "<br/><br/>"; }
 th { border: 6 px solid blue; border - collapse: collapse; } table { border - collapse: collapse; } td { border: 6 px solid black; border - collapse: collapse;
 <div id="show"> <table id="info"> <tr> <th><strong>First/Last Name</strong></th> <th><strong>Date of Birth</strong></th> <th><strong>Spoken Languages</strong></th> </tr> </table> </div>

intermediate has no value for one of the person on the list. intermediate对列表中的一个人没有价值。 It is by default set to undefined if the property is missing.如果缺少该属性,则默认设置为undefined

You can define a default value as part of the destructuring assignment您可以将默认值定义为解构赋值的一部分

let {
       spokenLanguages: {
          native = '',
          fluent = '',
          intermediate = ''
       }
    } = userList.people[i];

There are lots of ways to refactor the code to make it a bit more cleaner.有很多方法可以重构代码以使其更简洁。

 let userList = { "people": [ { firstName: "Fred", lastName: "Smith", dateOfBirth: 1980, spokenLanguages: { native: "English", fluent: "Spanish", intermediate: "Chinese" } }, { firstName: "Monica", lastName: "Taylor", dateOfBirth: 1975, spokenLanguages: { native: "Spanish", fluent: "English", intermediate: "French" } }, { firstName: "Maurice", lastName: "Edelson", dateOfBirth: 1992, spokenLanguages: { native: "English", fluent: "Spanish", } }, { firstName: "Kelly", lastName: "Lang", dateOfBirth: 1982, spokenLanguages: { native: "English", fluent: "German", intermediate: "Dutch" } } ] }; for (var i = 0; i < userList.people.length; i++) { let table = document.getElementById("info"); let newRow = table.insertRow(1); let cell = newRow.insertCell(0); let dobCell = newRow.insertCell(1); let langs = newRow.insertCell(2); const { firstName = '', lastName = '', dateOfBirth = '', spokenLanguages: { native = '', fluent = '', intermediate = '' } } = userList.people[i]; cell.textContent = `${firstName} ${lastName}`; dobCell.textContent = dateOfBirth; langs.textContent = `${native}, ${fluent}, ${intermediate}`; }

  • As you are already using es6 , you can rely on template literals to make your code more readable.由于您已经在使用es6 ,您可以依靠模板文字使您的代码更具可读性。
  • For every iteration you are inserting a new row and cell, so you don't really need to use += operator.对于每次迭代,您都将插入一个新行和单元格,因此您实际上并不需要使用+=运算符。

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

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