javascript 未定义(阅读'trim'),当文本区域中的数据行数超过100行时

uqjltbpv  于 2023-01-16  发布在  Java
关注(0)|答案(2)|浏览(76)

此代码在文本区域中解析数据,并且工作正常,但是当增加文本区域中的行数时,出现此错误:ncaught TypeError: Cannot read properties of undefined (reading 'trim')我尝试搜索,但不幸的是,我没有找到任何解决方案。
这是代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label style="display:block; width:100%">Original data
<textarea class="form-control" id="data" rows="5" cols="10" placeholder="ID's Phones Number:" name="msg">3343434340,Mahmoud Bahaa,male
3343434340,Mahmoud Bahaa,male
4343434340,Ahmed Farouk,male
3434343434,Mohamed Ali,male
1670142642,Ahmed Mohamed Mohamed,male
4343434340,أحمد حسن,male
3343434340,Mahmoud Bahaa,male
34340,Chemist Hosam,male
134016343434340,ﻋﻠﻲ الحويطي,male
1343434340,Moh Salah,male
4343434340,Ahmed Ayman,male
4343434340,Ahmed Farouk,male
4343434340,أحمد حسن,male
4343434340,Ahmed Ayman,male
134016343434340,ﻋﻠﻲ الحويطي,male
3434343434,Mohamed Ali,male
34340,Chemist Hosam,male
1343434340,Moh Salah,male
4343434340,Ŏś Ặɱặ Ĥặřpŷ,male
3343434340,Yaseen Oum,male
2343434340,Nahed Hunter,male
34340,Mohamed Abd El Nasser,male
34340,Mohamed Hafez Elbanna,male
2343434340,زوجى قرة عينى,male
8343434340,Mostafa Elabasy,male
603467378,Ahmed Elmalt,male
7343434340,Mohamed Gomaa,male
1143018863,Mohamed Nabil,male
4343434340,Mohamed Fahmy,male
34340,Yasmin Abd Elkader,male
631334223,Amin Zein Aminovic,male
2343434340,Asem Mohamed,male
8343434340,خويدم القرآن,male
2343434340,ḾǿĦặmmëd Sặdëiķ,male
4343434340,العربي سات,male
2343434340,Mòháměd YòUššéf,male
1667431477,Mohamed Aletr,male
7343434340,Khaled Abdelkader,male
3343434340,Gomaa Mohamed,male
3343434340,Ahmed Kottor,male
644148248,Nada Mohamed Mamdooh,male
34340,Mohamed Khayal,male
1064314488,Sohip Hekal,male
34340,Hassan Ashmawy,male
34340,Tarek Fouad,male
2343434340,Ali Elkhwagha,male
6343434340,Salem Shehab,male
1340134340,Sabri Debor,male
3343434340,Ahmed El Attar,male
1641147744,Yasser Arafa,male
3434343434,Mahmoud Shafik,male
8343434340,Abd ElFatah Shoman,male
6343434340,Abdo Ashraf,male
134023434343434,العالمي العالمي,male
3434343434,Nada Ayman,male
134016343434340,Hamada Hosny,male
3434343434,Thomas Azer,male
34340,Esam Sherif,male
1340134340,Nona Sad,male
134013343434340,Nana Elzanon,male
482337287,Mohamed Naguib,male
2343434340,Koki Ayad,male
6343434340,Ahmed Adel,male
2343434340,Ahmed Hassan,male
2343434340,Abdelrahman Ahmed,male
4343434340,Eslam Abd-ELhamid,male
34340,Haytham Mahmoud,male
1343434340,مصطفي رآفت,male
7343434340,Hamada Mustafa,male
34340,Hesham Ramadan,male
134014343434340,احمد النجار,male
4343434340,Mohsen El Menyawy,male
474336438,Kyrillos Bassem,male
134013343434340,محمد غانم,male
434041760,احمد ابوالفضل النجار,male
1340134340,زهرة الصبار,male
4343434340,ابوأمين ابوأمين,male
2343434340,احمد ابو عماد,male
3343434340,Mohamed Sayd Abaas,male
8343434340,BaSma OthMan,male
3343434340,Nouran Samak,male
134016343434340,Omar Hesham,male
1047746040,Dalia Mahmoud,male
34340,مصطفي حبيشي,male
4343434340,Salwa Hafez,male
34340,Sara Raouf,male
6343434340,رامي محمد ابو آسر,male
134023343434340,Mohamed Abd Elaty,male
4343434340,Ahmed Hamasa,male
624383632,TAmer Ali Hasnain,male
134012343434340,Ahmed Khaled,male
4343434340,Mohamed Nabil Elmory,male
1282463048,Mohamed Bahnasy,male
134023343434340,Mohamed Mega,male
34340,Mohamed Altantawy,male
2343434340,ɱŏĥặɱěd Śặíd,male
4343434340,Moamen Zakari,male
8343434340,Lobna Mohammed,male
3343434340,Mohamed Fathy,male
34340,Mohamed Foad,male
3434343434,السيد المكاوى,male
8343434340,Mohamed Selim,male
6343434340,Ahmed Sakr,male
3343434340,Elbasha Ibrahim Elbasha,male
3434343434,Haitham Elhfnawy,male
3343434340,Hassan Ramadan,male
2343434340,عوض توفيق,male
6343434340,Mostafa Heram,male
6343434340,Hany Abd Elmageed,male
3434343434,Reem Leila,male
1340134340,علي عيسي شوكت,male
134014343434340,Eslam Abdala,male
4343434340,هانى الشابورى,male
2343434340,Naglaa Elgazzar,male
486241713,Mohamed Kordy,male
4343434340,Mohamed El-alfy,male
134014343434340,Ømãř Mĥâmøŭđ,male
3343434340,Alaa Wahed,male
2343434340,Mostafa Farahat,male
1343434340,DeRaa Mohamed,male
3343434340,وائل البقري ابو روان,male
34340,Mohamed Magdy,male
1701174244,Ahmed Abo Mosllam,male
7343434340,Abdullah Moh Bekheet,male
3343434340,Abdulrhman Mohamad,male
636020443,Ayat Gamal,male
3343434340,Seif Said,male
7343434340,Ahmed Hossam,male
3434343434,Mohamed Said,male
8343434340,Mohamed Fawzy,male
616246434,Amr Hamza,male
34340,Mahmoud EL Essawy,male
7343434340,Kareem Essam,male
1437644816,Karen Nan,male
3343434340,Ahmed Salah,male
7343434340,Karam Abdelfatah,male
3343434340,هاني ربيع,male
834034301,GeEdo Ahmed,male
134012343434340,Ahmed M. Abd El-Aziz,male
2343434340,Ahmed Gado,male
134017343434340,ابراهيم السيد,male
7343434340,Mohamed Taha,male
2343434340,Ali Abd El Hady,male
134016343434340,Khames Elzokm,male
446136416,Mahmoud Mohsen,male
3434343434,Osama Elsadani,male
134014343434340,السيد ابو اياد,male
134012343434340,Nadia Elbaz,male
603821071,Islam Elsesy,male
134012343434340,Lamar Ehab,male
4343434340,Abd-Elrhman Ahmed,male
6343434340,السيد عبدالله,male
7343434340,حامد موسى,male
134026343434340,Ana Nour,male
4343434340,Abd Alazez,male
134014343434340,محمد زين العابدين حسين,male
134013343434340,Ranya Ahmed Salleh,male
34340,محمد الشال,male
8343434340,السعيد يونس,male
4343434340,Wael Abdelhameed,male
34340,Mohamed Adel,male
34340,Mohamed Alhussieny Mohamed,male
431623466,Amr Hazem,male
7343434340,Mohamed Elsaid,male
2343434340,Farida Haroun,male
7343434340,MŐstafa Àbo Ëlĥassen Mőhâmmêd,male
4343434340,Hatem Hamza,male
8343434340,خالد السمان,male
843134636,Mostafa Nakhla,male
134018343434340,متفائل في زمن الياس,male
134014343434340,Ahmed Yassin,male
34340,Dalia El-kashef,male
4343434340,احمد صالح,male
3434343434,Raef Kamal,male
2343434340,Môhámed Mâhmøùd,male
3434343434,Gina Raafat,male
1081634341,David Mamdouh,male
468488604,Yasmin El Saadany,male
734464317,Muhammad Magdy,male
134014343434340,Mahmoud Mahmoud,male
4343434340,Ahmed Zedan,male
3343434340,Marwa El Dessouky,male
4343434340,Mohamed Fawzy,male
6343434340,Yousry Fawzy,male
134016343434340,نورا صلاح,male
3434343434,Beshoy Nbe Albir,male
6343434340,Abdelrahman Sayed,male
134023343434340,Sara M. Abdallah,male
2343434340,EsRaa Matarawy,male
34340,Abdelrahman Mamdouh,male
3434343434,Ahmed Shaltout,male
3434343434,Karem Elfakey,male
3343434340,أحمد عزام,male
1340134340,Abdo Saied,male
2343434340,Ahmed Adel Bebars,male
7343434340,Ahmed Ismaeal,male
6343434340,Ahmed Elhady,male
1343434340,Shehab Yasser,male
34340,Eman Kamal,male
8343434340,Sameh Basha,male
7343434340,Timaa Zewel,male
1343434340,Hadeer Emara,male
4343434340,Bassêm Fathê,male
1438086126,Se-If A. ZaYed,male
34340,Khaled Reda,male
3343434340,Ahmed Khater,male
3343434340,Raef Badrous,male
3343434340,Ahmed Nasser,male
3434343434,Helmy Afifi,male
2343434340,Mhmd Mohamed,male
1348083011,Samar Hamdi,male
4343434340,Ali Allâm,male
8343434340,Mohamed Eltorke,male
4343434340,Wafaa Ali,male
2343434340,Nada M. RasMy,male
34340,Omar Ratep,male
4343434340,Mohamed Elbastaweisy,male
4343434340,ÀbđàlŁa Âziź,male
3343434340,انوشه صافي,male
2343434340,محمد مصطفى محمد عبدالعزيز,male
3343434340,Khaled Ahmed,male
1010868183,Abdelhamid Taha,male
3434343434,Ahmed Emad,male
1340234340,علاء حمزه,male
4343434340,Samy Mohammed,male
424214437,Mohamed Abed El Monem,male
436404807,SaMeh Mehmeh,male
1143030316,HaZem SheDeed,male
3343434340,Ahmed Hamza,male
8343434340,Haitham Elnemr,male
6343434340,Nada Nader,male
34340,MoniCa S. Mhawed,male
3343434340,Muhammad El Sherif,male
816234888,Noha Elkomy,male
8343434340,Amr Sayed,male
643074637,Fatma Mahmoud Abou Ali,male
2343434340,سلامه سمسم,male
1371634234,Ibrahim Negm,male
414118421,Tarek Khalil,male
134014343434340,عبد العزيز سعد,male
4343434340,Samir Elshamy,male
34340,Abdulrahman Jamal,male
2343434340,Abdelrhman Abdelfattah,male
6343434340,Mohamed Sophy,male
34340,اشرف الرفاعي,male
631643873,Ebram Adel,male
2343434340,Ayman Abdelazez,male
4343434340,Mona Mahmoud,male
443371248,Mohamed Hosny Mahmoud,male
2343434340,Mohamed Emam,male
3434343434,Mahmoud El Tayp,male
4343434340,Abdallah Ahmed,male
34340,سامح حمدان,male
1174412706,Amr Elkholy,male
4343434340,Ahmed Al Nope,male
34340,Bosha Magdy,male
6343434340,Fawzy Adel,male
134012343434340,Ahemad Kader ELasle,male
1343434340,Mariam Wael,male
7343434340,Sayed Maher,male
2343434340,Nagy,male
4343434340,Yasser Elzegheibi,male
4343434340,Wael Gamal,male
3343434340,Hassan Anwar Kadry,male
4343434340,Ahmed Yassen,male
3434343434,Fathi Shaaban,male
4343434340,Eslam Slamaa Elmasry,male
2343434340,محمود مضيها,male
7343434340,Heba Mostafa,male
134028343434340,مصطفي محمود محمود عبدالله,male
1043348444,Hassan Salah,male
7343434340,Zaki Zahra,male
2343434340,Mohsen Koura,male
4343434340,Mohamed Maher,male
134014343434340,منصور حسن,male
4343434340,Mohamed Hamza,male
2343434340,Ibrahim Elsayed,male
3343434340,Abd Elhamed Ramadan,male
3343434340,Youssif SH Rif,male
34340,ALi EL Masry,male
2343434340,Mohamed Elmasry,male
4343434340,George Sameir,male
134023343434340,Ahmed Youssif,male
4343434340,Abanoub Melad,male
1336168143,Ahmed Mahgoub,male
2343434340,عاصم القصاص,male
1681267873,Marwa Kamel,male
134023343434340,محمد البسيوني العقباوي,male
2343434340,Omar Fouda,male
34340,Eslam Helal,male
3434343434,Mohamed Zayat,male
6343434340,Ola Esmail,male
134027343434340,Ahmed Maher Ibrahim,male
7343434340,Ÿøüsêf Hâssäñ Ømrâñ,male
4343434340,Mostafa Mohamad,male
7343434340,وليد سلومه,male
673032377,Heba Ouda,male
2343434340,Saad Momtaz,male
6343434340,Mohamed Ragab,male
3434343434,Abanob Mamdoh,male
4343434340,Maha Mohamed,male
134014343434340,Mahmoud Mohmed,male
34340,Ibraham Matar,male
1062112860,Manar Mohamed,male
6343434340,Hala Hamdy,male
1340134340,هيثم علي,male
4343434340,Zeka Mohamed,male
723710311,Hossam Adly,male
4343434340,Youssef Ibrahem,male
3343434340,Ahmed Zezoo,male
1488736883,Hanafy Elshwekh,male
134018343434340,Mohamed Gooma,male
8343434340,Abdelrhman Ali,male
34340,محمد عبدالكريم,male
7343434340,Sameh Elgzar,male
748383346,Mohamed Aboalkhair,male
2343434340,Nessma Zaki,male
134024343434340,Amr Abdelfattah Mohmed,male
3343434340,Ali Elatmany,male
134014343434340,محمود ابو سرحان,male
6343434340,Cap Ahmed B Alhaweey,male
4343434340,Mohamed Elarabey,male
6343434340,Kareem Ahmed,male
8343434340,Ashraf Elbendary,male
34340,Mahmoud Elkallaf,male
3343434340,Mokhtar Khaled,male
134024343434340,Mohamed Ayad,male
4343434340,Hadeer Ahmed,male
3343434340,Samir Felfela,male
4343434340,Abdel Fattah Masoud,male
34340,Hisham Etsh,male
2343434340,Lamisa Essam,male
34340,أشرف عبدالنور,male
2343434340,Mohamed Abu Elshikh,male
134014343434340,Faiza Fathy,male
34340,Mohamed Bagha,male
134013343434340,Kanzy Kanzy,male
8343434340,Ali Lolo,male
7343434340,Mohamed Saad,male
434018683,Mai Adel,male
34340,M Yehia Elgammal,male
7343434340,Mahmed Ali,male
4343434340,Mohamed Tag,male
4343434340,Albert Mosa,male
2343434340,Hala El-Shenawy,male
2343434340,Hossam Hosny,male
1036774474,Sayed Mohy Zico,male
134014343434340,Abdelaziz Mahmoud,male
8343434340,Magdy Adel,male
3343434340,علاء سمير الغزولي,male
3434343434,Mario Mina,male
34340,Hassan Oransa,male
3434343434,Girgis Silem,male
726188614,Walaa Hussein,male
1340134340,Saeed Almasry Saif,male
134023434343434,ابو مالك عبد العزيز,male
6343434340,Ahmed Elsayed,male
2343434340,Ayman Nabil,male
2343434340,علاء ابوريه,male
1742402434,عمرو صقر,male
134014343434340,Mohamed Emed Elder,male
2343434340,Andrew Gerges,male
7343434340,Hamdy Refaat,male
8343434340,Mohesn Noh,male
1343434340,Hala Mohamed,male
3343434340,Said Fiala,male
134016343434340,رامى منسى,male
2343434340,Èŝłăm ḾõĦậměđ,male
3343434340,عبدالله شوقى الطيب,male
134014343434340,محمد الخطيب الخطيب,male
34340,Basem Magdy,male
6343434340,Ehab Abd El-Galel,male
8343434340,Mohammed Tomas,male
8343434340,Shimaa Ahmed,male
1340134340,Diana Anwer,male
2343434340,Mohamed Elshireef,male
1343434340,MOstafa MAgO,male
134023343434340,Fayez Ramadan,male
1343434340,Muhammad Fatouh Eraki,male
8343434340,Mohamed Elshafey,male
837383434,Mahmoud Saeed Sayd,male
3434343434,Hashem Mostafa,male
1444633128,ربيع عبد النبي,male
34340,Amir Mohamed,male
134022343434340,Samy ELsafaey,male
1340134340,محمود صبرى البحيرى البحيرى,male
2343434340,Fares Elsokary,male
4343434340,Dr-Mohamed Newera,male
1343434340,Suzan Rabie,male
2343434340,Ahmed Sami,male
34340,Eng-Mohamed Orban,male
2343434340,نغم عاطف,male
832384174,Mohamed Magdy,male
602047837,Ebram Boshra,male
8343434340,Omar Mohamed,male
6343434340,MArk Emad,male
3434343434,Abdalla Fahmy,male
34340,Amira Abdallah,male
3343434340,Hesham Sobhy,male
8343434340,Mina Mimi,male
2343434340,Ahmed Adel Soliman,male
6343434340,Hånï W Mõhāmëd Tîtøø,male
4343434340,Hassan Abass,male
1343434340,Yousry Othman,male
3343434340,Ahmed Bisher,male
4343434340,Mohammed Mahmmod,male
2343434340,Abdulrahman Sliem,male
34340,Amir Mk Sh,male
3343434340,Ěśł Ặɱ Íßřặĥíɱ,male
3343434340,Nasser Helmy,male
1343434340,Ahmed Mohamedy,male
34340,Mostafa Ramadan,male
34340,Mostafa Kamel,male
34340,Mostafa Kamel,male
134013343434340,Mina Noshy,male
134013343434340,Mina Noshy,male
34340,Mostafa Ramadan,male
446464326,Ahmed Badawy,male
3343434340,Yossef Khalf,male
3343434340,Yossef Khalf,male
6343434340,همس الكلام,male
134012343434340,Islam Smsm,male
4343434340,Osama Ahmed,male
1340134340,Ahmed Helmy Abo Yassen,male
2343434340,اسلام السعيد محمد,male
3343434340,معاذ أبوسعد,male
8343434340,أيسل منصور,male
2343434340,Ahmed Amer,male
8343434340,صبرى سلامه,male
7343434340,عبده سالم,male
1477802184,R Amdan Rashad,male
34340,Ahmed Nagah,male
4343434340,Abdo Ahmed,male
134012343434340,Mazen Ahmed Elsaadany,male
2343434340,Abdelmoniem Elsayed Mahmoud,male
34340,Monir MOrad,male
1343434340,Ziad Korin,male
34340,Eng Islam Maged,male
3434343434,Mo'men Tharwat,male
7343434340,محمد رفعت,male
1343434340,Mohamed Hussein,male
4343434340,Afnan Bahaa,male
8343434340,Mansour Lawyer,male
3343434340,Noor Alhoda,male
8343434340,Ihab L Sayed,male
1343434340,Mohamed Hussein,male
4343434340,Afnan Bahaa,male
8343434340,Mansour Lawyer,male
3343434340,Noor Alhoda,male
8343434340,Ihab L Sayed,male
7343434340,Shenouda Adel,male
34340,Mohamed Bolbol,male
3434343434,Mona Mousa,male
7343434340,Shenouda Adel,male
34340,Mohamed Bolbol,male
3434343434,Mona Mousa,male
1807743168,Mahmoud Moustafa,male
34340,ALaa Elshamy,male
2343434340,Ahmed M. Fawzy,male
3434343434,Mohamed El-fawal,male
2343434340,Amr Abdelaal,male
134013343434340,Mohamed S Marey,male
34340,Ehab Shabana,male
8343434340,Eman M. Shalash,male
8343434340,رياض الصالحين رياضالصالحين,male
134023343434340,حسام الدين,male
2343434340,Amr Hassan Fahmy,male
3343434340,Mahmoud Mohamed,male
3343434340,هاني محمد,male
1323343020,Karim Abo Galwa,male
3343434340,Samir Morcos,male
34340,Sally Elsayed,male
638462844,Haidy Safwat,male
134012343434340,Yara Tarek,male
3343434340,Mohamed Morsi,male
634444342,Eng Mohamed Adel,male
134016343434340,محمد الوكيل,male
4343434340,Sâmặř ŚáĹáh,male
34340,Emad Hamdy,male
1340234340,Ahmed Elbassiony,male
34340,Sameh Mina,male
1274373246,Shereen Salama,male
2343434340,Ahmed Enawy,male
1343434340,Ahmed Hassan,male
6343434340,Mahmoud Gamel,male
3343434340,Mahmoud Mohsen,male
1370248718,Mohanad Mostafa Mounir,male
1343843468,MuStafa MohaMed,male
1636776417,Mohamed Zinhom,male
2343434340,Waleed A. Ryan,male
4343434340,محمدابراهيم عدس,male
34340,Toni Noshy,male
4343434340,Hisham Ramadan,female
6343434340,Abdalla Mahmoud,female
2343434340,Koko Magdy,female
630721447,Mahmoud Nawar,female
34340,Adel Elbltagi,female
6343434340,محمد نادى سالم,female
743086674,Ahmed Abdalla,female
4343434340,Hany Mostafa,female
8343434340,Mahmoud Shatla,female
423136421,Ahmed Mahmoud,female
2343434340,Mahmoud Moustafa Fathallah,female
6343434340,Hamdy Allam,female
1340134340,Sahar Abdallah,female
614344376,Osama Mohamed,female
134014343434340,Horeaa Mohamed Farag,female
1340134340,Mohamed Allam,female
34340,Ahmed Genedy,female
1343434340,Mohamed Alaa,female
2343434340,Fathy Emad,female
34340,Ahmed Hanafy,female
3343434340,رشدي الرفاعي,female
636641420,Roka Maged,female
8343434340,علاء محمد,female
4343434340,Abdo Ismail,female

</textarea>
</label>
<label class="results">Just names
<textarea style="display:block" id="justNames" name="justNames"></textarea>
</label>
<label class="results">IDs and names
<textarea style="display:block" id="idsNames" name="idsNames"></textarea>
</label>
<label class="results">Just genders
<textarea style="display:block" id="justGenders" name="justGenders"></textarea>
</label>
<label class="results">IDs and genders
<textarea style="display:block" id="idsGenders" name="idsGenders"></textarea>
</label>
<label class="results">Names and genders
<textarea style="display:block" id="namesGenders" name="namesGenders"></textarea>
</label>
<label class="results">IDs, names and genders
<textarea style="display:block" id="idsNamesGenders" name="idsNamesGenders"></textarea>
</label>

<script>
var results = [];
var data = $('#data').val().trim();

if(data) {
  // Create an array by splitting the textarea.value by newline
  data = data.split(/\r?\n/);
 
  // Loop through indivdual rows, and split by comma / ,
  for(var i = 0; i < data.length; i++) {
    var temp = data[i].trim();

    if(temp) {
      // Create an array from the values in the current line
      temp = temp.split(",");

      // Separate the first and last names
      var pairs = temp[1].split(/ (.*)/s);

      var personsObj = {};
      personsObj.id = temp[0];
      personsObj.firstName = pairs[0].trim();
      personsObj.lastName = pairs[1].trim();
      personsObj.gender = temp[2].trim();
      
      results.push(personsObj);
    }
  }
}

var tmpNames = "", tmpIdsNames = "",
tmpGenders = "", tmpIdsGenders = "",
tmpNamesGenders = "", tmpIdsNamesGenders = "";

for(var i = 0; i < results.length; i++) {
    tmpNames += results[i].firstName+ "\r\n";
  tmpIdsNames += results[i].id + "," + results[i].firstName + "\r\n";
  tmpGenders += results[i].gender + "\r\n";
  tmpIdsGenders += results[i].id + "," + results[i].gender + "\r\n";
  tmpNamesGenders += results[i].firstName + "," + results[i].gender + "\r\n";
  tmpIdsNamesGenders += results[i].id + "," + results[i].firstName + "," + results[i].gender + "\r\n";
}

$("#justNames").val(tmpNames);
$("#idsNames").val(tmpIdsNames);
$("#justGenders").val(tmpGenders);
$("#idsGenders").val(tmpIdsGenders);
$("#namesGenders").val(tmpNamesGenders);
$("#idsNamesGenders").val(tmpIdsNamesGenders);
</script>

该示例工作良好:fiddle 1
当文本区域中的行超过100时,此示例不起作用,并出现错误:fiddle 2

66bbxpm5

66bbxpm51#

存在没有姓氏的数据。

...
2343434340,Nagy,male
...

您正尝试在第17行获取全名并用空格分隔它

Line 17: var pairs = temp[1].split(/ (.*)/s);

因此,当它解析没有姓氏的数据时,pairs变量将返回内部只有一个元素的数组值。

Line 20: personsObj.id = temp[0];
Line 21: personsObj.firstName = pairs[0].trim();
Line 22: personsObj.lastName = pairs[1].trim(); // Error triggered, because `pairs` variable only return one element
Line 23: personsObj.gender = temp[2].trim();

**解决方案,**您可以先检查pairs[1](您可以使用ternary operator),这样您就可以使用下面的代码更改第22行的代码

Line 22: personsObj.lastName = pairs[1] ? pairs[1].trim() : '';

因此,当没有显示姓氏时,它将被替换为空字符串''

zkure5ic

zkure5ic2#

此行中没有姓氏:

2343434340,Nagy,male

因此personsObj.lastName = pairs[1].trim();将抛出一个错误,因为pairs[1]undefined

相关问题