简体   繁体   English

如何修剪 javascript 数组

[英]How to trim a javascript array

I want to trim an array after splitting my springs我想在拆分我的弹簧后修剪一个数组

 data = ',VALDESC::Value Date/PDESC::Description/REFNO::Txn Ref/REF.FT::Narration/POST::Booking Date/DR.AMT::Debit/CR.AMOUNT::Credit/BALANCE::Closing Balance,"","14 MAY 19 " "Cash Withdrawal " "TT19134V74S7 " " " "14 MAY 19" "4000" "" "-4000","","21 MAY 19 " "Bulk Payment (NAMBU" "FT19141911080" " " "21 MAY 19" "" "71630.33" "90477.42","BEIGN STAFF AMY 2019 SAL","21 MAY 19 " "Bulk Payment (NAMBU" "FT19141912082" " " "21 MAY 19" "2000" "" "88477.42","STAFF COOP MAY 2019","22 MAY 19 " "Cash Withdrawal " "TT1914247YZ5 " " " "22 MAY 19" "5000" "" "83477.42","","24 MAY 19 " "POS Purchase " "FT19144536496" " " "24 MAY 19" "3400" "" "80077.42","@ MEGA CHICKEN RESTAUR LA LANG STAN","" "" "" "" "" "" "" "" "053588 ","24 MAY 19 " "POS Purchase " "FT19144019378" " " "24 MAY 19" "1400" "" "78677.42","@ MEGA CHICKEN RESTAUR LA LANG STAN","" "" "" "" "" "" "" "" "160419 ","27 MAY 19 " "ATM Cash Withdrawal" "FT19147487249" " " "27 MAY 19" "2000" "" "76677.42","@ 1 IKOYI ROAD OBALENDE LAGOS LANG ","" "" "" "" "" "" "" "" "STAN 008029 ","","","","","",""," 76677.42" "" ""' const lines = data.split('","'); for (let i = 1; i < lines.length; i++) { var g = lines[i].trim(); } console.log(g); const headerline = lines[0]; const splitHeaderline = headerline.split('/');

I think you want to "remove" blank element on the array and not trim them.我认为您想“删除”数组上的空白元素而不是修剪它们。

 data = ',VALDESC::Value Date/PDESC::Description/REFNO::Txn Ref/REF.FT::Narration/POST::Booking Date/DR.AMT::Debit/CR.AMOUNT::Credit/BALANCE::Closing Balance,"","14 MAY 19 " "Cash Withdrawal " "TT19134V74S7 " " " "14 MAY 19" "4000" "" "-4000","","21 MAY 19 " "Bulk Payment (NAMBU" "FT19141911080" " " "21 MAY 19" "" "71630.33" "90477.42","BEIGN STAFF AMY 2019 SAL","21 MAY 19 " "Bulk Payment (NAMBU" "FT19141912082" " " "21 MAY 19" "2000" "" "88477.42","STAFF COOP MAY 2019","22 MAY 19 " "Cash Withdrawal " "TT1914247YZ5 " " " "22 MAY 19" "5000" "" "83477.42","","24 MAY 19 " "POS Purchase " "FT19144536496" " " "24 MAY 19" "3400" "" "80077.42","@ MEGA CHICKEN RESTAUR LA LANG STAN","" "" "" "" "" "" "" "" "053588 ","24 MAY 19 " "POS Purchase " "FT19144019378" " " "24 MAY 19" "1400" "" "78677.42","@ MEGA CHICKEN RESTAUR LA LANG STAN","" "" "" "" "" "" "" "" "160419 ","27 MAY 19 " "ATM Cash Withdrawal" "FT19147487249" " " "27 MAY 19" "2000" "" "76677.42","@ 1 IKOYI ROAD OBALENDE LAGOS LANG ","" "" "" "" "" "" "" "" "STAN 008029 ","","","","","",""," 76677.42" "" ""' const lines = data.split('","') .filter(el => !!el) // Remove blank elements .map(el => el.trim()) // Remove white space before and after console.log(lines)

Although I wrote some code to parse that format according to my best guess about the structure, it's not pretty, and that is mostly due to the craziness of that format.尽管我根据我对结构的最佳猜测编写了一些代码来解析该格式,但它并不漂亮,这主要是由于该格式的疯狂。 If you have any control over it, I would suggest that you export something more reasonable.如果您对此有任何控制权,我建议您导出更合理的内容。 But I made a number of assumptions and ended up with this code:但我做了一些假设,最终得到了这段代码:

 const process = (data) => { const headerEnd = ',"",' const end = data .indexOf (headerEnd) const rawHeader = data .slice (1, end) const headers = rawHeader .split ('/') .map (s => s.split('::')) .map (([name, desc]) => ({name, desc})) .concat ({name: '', desc: 'Unknown'}) const len = headers .length; const fields = data.slice(end + headerEnd.length) .split(/\\t|,/).map(s => s.slice(1, -1).trim()) const body = Array (Math .ceil (fields .length / len)) .fill () .map ((_, i) => fields. slice (i * len, i * len + len)) .map ( fields => headers .reduce ( (a, {name, desc}, i) => ({...a, [desc]: fields[i]}), {} ) ) return {headers, body} } const data = ',VALDESC::Value Date/PDESC::Description/REFNO::Txn Ref/REF.FT::Narration/POST::Booking Date/DR.AMT::Debit/CR.AMOUNT::Credit/BALANCE::Closing Balance,"","14 MAY 19 " "Cash Withdrawal " "TT19134V74S7 " " " "14 MAY 19" "4000" "" "-4000","","21 MAY 19 " "Bulk Payment (NAMBU" "FT19141911080" " " "21 MAY 19" "" "71630.33" "90477.42","BEIGN STAFF AMY 2019 SAL","21 MAY 19 " "Bulk Payment (NAMBU" "FT19141912082" " " "21 MAY 19" "2000" "" "88477.42","STAFF COOP MAY 2019","22 MAY 19 " "Cash Withdrawal " "TT1914247YZ5 " " " "22 MAY 19" "5000" "" "83477.42","","24 MAY 19 " "POS Purchase " "FT19144536496" " " "24 MAY 19" "3400" "" "80077.42","@ MEGA CHICKEN RESTAUR LA LANG STAN","" "" "" "" "" "" "" "" "053588 ","24 MAY 19 " "POS Purchase " "FT19144019378" " " "24 MAY 19" "1400" "" "78677.42","@ MEGA CHICKEN RESTAUR LA LANG STAN","" "" "" "" "" "" "" "" "160419 ","27 MAY 19 " "ATM Cash Withdrawal" "FT19147487249" " " "27 MAY 19" "2000" "" "76677.42","@ 1 IKOYI ROAD OBALENDE LAGOS LANG ","" "" "" "" "" "" "" "" "STAN 008029 ","","","","","",""," 76677.42" "" ""' console .log ( process (data) )

It generates output like this:它生成如下输出:

{
  headers: [
    {
      name: 'VALDESC',
      desc: 'Value Date'
    },
    {
      name: 'PDESC',
      desc: 'Description'
    },
  // ...
  ],
  body: [
    {
      'Value Date': '14 MAY 19',
      Description: 'Cash Withdrawal',
      'Txn Ref': 'TT19134V74S7',
      Narration: '',
      'Booking Date': '14 MAY 19',
      Debit: '4000',
      Credit: '',
      'Closing Balance': '-4000",
      Unknown: ''
    },
    {
      'Value Date': '21 MAY 19',
      Description: 'Bulk Payment (NAMBU',
      'Txn Ref': 'FT19141911080',
      Narration: '',
      'Booking Date': '21 MAY 19',
      Debit: '',
      Credit: '71630.33',
      'Closing Balance': '90477.42',
      'Unknown': 'BEIGN STAFF AMY 2019 SAL'
    },
    // ...
  ]
}

You could change the keys here from the longer descriptive phrase to the shorthand, simply by replacing您可以将此处的键从较长的描述性短语更改为速记,只需替换

        (a, {name, desc}, i) => ({...a, [desc]: fields[i]}),

with

        (a, {name, desc}, i) => ({...a, [name]: fields[i]}),

And obviously if you have no need of the headers, you can return simply body .显然,如果您不需要标题,则可以简单地返回body

But again, I would not use code like this if I had any hope of altering the input format.但同样,如果我有任何希望改变输入格式,我不会使用这样的代码。

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

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