创建新对象时进行解构和重命名

时间:2019-09-04 12:47:07

标签: javascript node.js

我们有lambda可以从S3(JSON文件)中读取文件并进行处理。 JSON对象包含许多字段,但我们只需要其中几个字段。我正在尝试对其进行分解,因此我能够成功地做到这一点。但是,在创建新对象以将其推送到数据库时,我们还需要重命名一些属性。我们的传入对象如下所示:

[
 {
   "title": "A Prayer for the Dying",
   "titleNoPrefix": "Prayer for the Dying",
   "seriesID": 153,
   "publisherID": 4030967,
   "territoryID": 9,
   "subtitle": "",
   "media": "E Publication",
   "format": "Standard ePub",
   "ean": "978-1-4532-0046-9",
   "ean13": 9781453200469,
   "isbn": "1-4532-0046-0",
   "isbn10": "1453200460",
   "primaryformatean": 9781453200469,
   "editiontype": "",
   "editionnumber": "",
   "series": "The Martin Fallon Novels",
   "seriesvol": 2,
   "bisacstatus": "Active",
   "pagecount": 166,
   "pubdate": "20100622",
   "territories": "",
   "returns": "Nonreturnable",
   "returnrestrictions": "No, not returnable",
   "copyrightyear": 1973,
   "classificationtype": "",
   "agerange": "",
   "graderange": "",
   "language1": "English",
   "language2": "",
   "twtypctexcerptrights": "No",
   "acctrightsrule": "StandardRights",
   "appleepubvnotes": "The back matter has been updated in this version.",
   "bookboard": "No",
   "bundle": "No",
   "capstone": "No",
   "ehistory": "No",
   "fixedlayoutpdf": "No",
   "frontlist": "No",
   "iconic": "No",
   "nbafinalist": "No",
   "projectphase": "Proofreading",
   "short": "No",
   "texttospeech": "No",
   "totalboox": "No",
   "youngreaders": "No",
   "audiences": ""
},
 {
   "title": "A Prayer for the Dying - Part 2",
   "titleNoPrefix": "Prayer for the Dying",
   "seriesID": 153,
   "publisherID": 4030967,
   "territoryID": 9,
   "subtitle": "",
   "media": "E Publication",
   "format": "Standard ePub",
   "ean": "978-1-4532-0046-9",
   "ean13": 9781453200469,
   "isbn": "1-4532-0046-0",
   "isbn10": "1453200460",
   "primaryformatean": 9781453200469,
   "editiontype": "",
   "editionnumber": "",
   "series": "The Martin Fallon Novels",
   "seriesvol": 2,
   "bisacstatus": "Active",
   "pagecount": 166,
   "pubdate": "20100622",
   "territories": "",
   "returns": "Nonreturnable",
   "returnrestrictions": "No, not returnable",
   "copyrightyear": 1973,
   "classificationtype": "",
   "agerange": "",
   "graderange": "",
   "language1": "English",
   "language2": "",
   "twtypctexcerptrights": "No",
   "acctrightsrule": "StandardRights",
   "appleepubvnotes": "The back matter has been updated in this version.",
   "bookboard": "No",
   "bundle": "No",
   "capstone": "No",
   "ehistory": "No",
   "fixedlayoutpdf": "No",
   "frontlist": "No",
   "iconic": "No",
   "nbafinalist": "No",
   "projectphase": "Proofreading",
   "short": "No",
   "texttospeech": "No",
   "totalboox": "No",
   "youngreaders": "No",
   "audiences": ""
}

]

将文件读入书本对象后,这就是我要执行的操作

books.map((book) => {
                    let subBook = (({
                      ean13,
                      title,
                      publisher,
                      imprint,
                      format,
                      series
                    }) => ({
                            ean13,
                            title,
                            publisher,
                            imprint,
                            format,
                            series
                    }))(book);

因此在subBook对象中,我们需要将ean13命名为primary_isbn,并且title需要命名为“ booktitle”

我尝试跟随但没有成功

books.map((book) => {
                    console.log(`Book Item : ${JSON.stringify(book)}`)
                    let subBook = (({
                      ean13: primaryIsbn,
                      title,
                      publisher,
                      imprint,
                      format,
                      series
                    }) => ({
                            ean13,
                            title,
                            publisher,
                            imprint,
                            format,
                            series
                    }))(book);

预先感谢

3 个答案:

答案 0 :(得分:1)

books.map((book) => {
                    console.log(`Book Item : ${JSON.stringify(book)}`)
                    let subBook = (({
                      ean13,
                      title,
                      publisher,
                      imprint,
                      format,
                      series
                    }) => ({
                            primary_isbn: ean13,
                            booktitle: title,
                            publisher,
                            imprint,
                            format,
                            series
                    }))(book);

答案 1 :(得分:0)

您需要在分解结构中重命名,然后在返回的对象中使用重命名的属性名称,如下所示:

let subBook = (({ean13: primaryIsbn, title: booktitle, publisher, ...}) => ({
  primaryIsbn,
  booktitle,
  publisher,
  ...
}))(book);

答案 2 :(得分:0)

在您的示例中,您没有在返回的对象中使用重命名的属性。

您可以映射书籍并重命名属性,而无需创建临时subBook变量,如下所示:

const books = [{
"title": "A Prayer for the Dying",
"titleNoPrefix": "Prayer for the Dying",
"seriesID": 153,
"publisherID": 4030967,
"territoryID": 9,
"subtitle": "",
"media": "E Publication",
"format": "Standard ePub",
"ean": "978-1-4532-0046-9",
"ean13": 9781453200469,
"isbn": "1-4532-0046-0",
"isbn10": "1453200460",
"primaryformatean": 9781453200469,
"editiontype": "",
"editionnumber": "",
"series": "The Martin Fallon Novels",
"seriesvol": 2,
"bisacstatus": "Active",
"pagecount": 166,
"pubdate": "20100622",
"territories": "",
"returns": "Nonreturnable",
"returnrestrictions": "No, not returnable",
"copyrightyear": 1973,
"classificationtype": "",
"agerange": "",
"graderange": "",
"language1": "English",
"language2": "",
"twtypctexcerptrights": "No",
"acctrightsrule": "StandardRights",
"appleepubvnotes": "The back matter has been updated in this version.",
"bookboard": "No",
"bundle": "No",
"capstone": "No",
"ehistory": "No",
"fixedlayoutpdf": "No",
"frontlist": "No",
"iconic": "No",
"nbafinalist": "No",
"projectphase": "Proofreading",
"short": "No",
"texttospeech": "No",
"totalboox": "No",
"youngreaders": "No",
"audiences": ""
}];

const formattedBooks = books.map(({
  ean13: primaryISBN,
  title: booktitle,
  publisher,
  imprint,
  format,
  series
}) => ({primaryISBN, booktitle, publisher, imprint, format, series}));

console.log(formattedBooks);