第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從文本文件讀取數(shù)據(jù)并將其存儲在對象數(shù)組中 - NodeJS

從文本文件讀取數(shù)據(jù)并將其存儲在對象數(shù)組中 - NodeJS

FFIVE 2023-11-02 17:12:18
客觀的我想制作這個程序,以便它可以從文本文件中讀取數(shù)據(jù)并將數(shù)據(jù)存儲到Array of Objects.下面顯示的格式是該文本文件將采用的格式,如果可能的話,應修剪逗號后面的空格,但不是完整的字符串。因此,當該程序運行時,它將把內(nèi)容推送到一個數(shù)組中,該數(shù)組稍后可以在程序中用作array.map().示例文本文件//file.txt (Note: The main file will always be in this exact format)John Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077 Jane Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077Arlong Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077Mark Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077Bill Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077// format: [ firstname lastname, date of birth (MM/DD/YYYY), Address, City, State, Zip]期望輸出array = [  {    firstname: "John",    lastname: "Doe",    dob: '01/30/2000',    address: 'This is the Address',    city: "Los Angeles",    state: "CA",    zip: "70077"  },  {    firstname: "Jane",    lastname: "Doe",    dob: '01/30/2000',    address: 'This is the Address',    city: "Los Angeles",    state: "CA",    zip: "70077"  },  {    firstname: "Arlong",    lastname: "Doe",    dob: '01/30/2000',    address: 'This is the Address',    city: "Los Angeles",    state: "CA",    zip: "70077"  }] //and so on
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經(jīng)驗 獲得超18個贊

嘗試這個


您可以獲得 csv 解析器,但如果您確定這是格式,則可以節(jié)省一些千字節(jié)


const csv = `John Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077 

Jane Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077

Arlong Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077

Mark Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077

Bill Doe, 01/30/2000, This is the Address, Los Angeles, CA, 70077`


const obj = csv.split(/\n/).map(line => {

  const [name, dob, Address, City, State, Zip] = line.split(", ")

  const [firstname, lastname] = name.split(" ");

  return { firstname, lastname, dob, Address, City, State, Zip: Zip.trim() }

});


console.log(obj)


查看完整回答
反對 回復 2023-11-02
  • 1 回答
  • 0 關(guān)注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號