如何过滤多维未编制索引的Javascript数组中的匹配记录?

时间:2018-04-16 12:54:46

标签: javascript multidimensional-array

我使用Google脚本界面编写一个脚本,使用Javascript操作相关的电子表格。

我有一张只包含交易的表格,我已将数据加载到数组中。该数组称为 allTrans(),如下所示:

[
  [Account No., Acc. Name, O/R, Ack. Receipt, Date, Description, Category, Credit, Debit, P. Method, Chq. Date, Chq. No., Bank Name, Deposited],
  [0001, Snow, , , Thu Apr 12 20:24:41 GMT+08:00 2018, H.O.A. dues for month of April, HOA Dues, , 400.0, , , , , ],
  [0002, Two, , , Thu Apr 12 20:24:41 GMT+08:00 2018, H.O.A. dues for month of April, HOA Dues, , 400.0, , , , , ],
  [0003, Three, , , Thu Apr 12 20:24:41 GMT+08:00 2018, H.O.A. dues for month of April, HOA Dues, , 400.0, , , , , ],
  [0004, Four, , , Thu Apr 12 20:24:41 GMT+08:00 2018, Monthly accrued interest @ 12% per annum (1% per month) on ageing balance more than 3 months., Int. Charge, , 4.0, , , , , ],
  [0004, Four, , , Thu Apr 12 20:24:42 GMT+08:00 2018, H.O.A. dues for month of April, HOA Dues, , 400.0, , , , , ],
  [0005, Five, , , Thu Apr 12 20:24:42 GMT+08:00 2018, Monthly accrued interest @ 12% per annum (1% per month) on ageing balance more than 3 months., Int. Charge, , 8.0, , , , , ],
  [0005, Five, , , Thu Apr 12 20:24:42 GMT+08:00 2018, H.O.A. dues for month of April, HOA Dues, , 400.0, , , , , ],
  [0006, Six, , , Thu Apr 12 20:24:43 GMT+08:00 2018, Monthly accrued interest @ 12% per annum (1% per month) on ageing balance more than 3 months., Int. Charge, , 12.0, , , , , ],
  [0006, Six, , , Thu Apr 12 20:24:43 GMT+08:00 2018, H.O.A. dues for month of April, HOA Dues, , 400.0, , , , , ]
]

我需要能够过滤属于某个帐号的所有交易(让我们说帐号' 0004',以便我可以将它们包含在他们的帐户对帐单中,但我可以&如果您没有要使用的key:value对,请找到有关如何执行此操作的任何示例。

请让我指出正确的方向,让我开始。

2 个答案:

答案 0 :(得分:2)

使用Array.prototype.filter()过滤Account No(数组的第一个元素)



let arr = [
  ['Account No.',' Acc. Name',' O/R',' Ack. Receipt',' Date',' Description',' Category',' Credit',' Debit',' P. Method',' Chq. Date',' Chq. No.',' Bank Name',' Deposited'],
  ['0001',' Snow',' ',' ',' Thu Apr 12 20:24:41 GMT+08:00 2018',' H.O.A. dues for month of April',' HOA Dues',' ',' 400.0',' ',' ',' ',' ',''],
  ['0002',' Two',' ',' ',' Thu Apr 12 20:24:41 GMT+08:00 2018',' H.O.A. dues for month of April',' HOA Dues',' ',' 400.0',' ',' ',' ',' ','' ],
  ['0003',' Three',' ',' ',' Thu Apr 12 20:24:41 GMT+08:00 2018',' H.O.A. dues for month of April',' HOA Dues',' ',' 400.0',' ',' ',' ',' ','' ],
  ['0004',' Four',' ',' ',' Thu Apr 12 20:24:41 GMT+08:00 2018',' Monthly accrued interest @ 12% per annum (1% per month) on ageing balance more than 3 months.',' Int. Charge',' ',' 4.0',' ',' ',' ',' ','' ],
  ['0004',' Four',' ',' ',' Thu Apr 12 20:24:42 GMT+08:00 2018',' H.O.A. dues for month of April',' HOA Dues',' ',' 400.0',' ',' ',' ',' ','' ],
  ['0005',' Five',' ',' ',' Thu Apr 12 20:24:42 GMT+08:00 2018',' Monthly accrued interest @ 12% per annum (1% per month) on ageing balance more than 3 months.',' Int. Charge',' ',' 8.0',' ',' ',' ',' ',' '],
  ['0005',' Five',' ',' ',' Thu Apr 12 20:24:42 GMT+08:00 2018',' H.O.A. dues for month of April',' HOA Dues',' ',' 400.0',' ',' ',' ',' ','' ],
  ['0006',' Six',' ',' ',' Thu Apr 12 20:24:43 GMT+08:00 2018',' Monthly accrued interest @ 12% per annum (1% per month) on ageing balance more than 3 months.',' Int. Charge',' ',' 12.0',' ',' ',' ',' ','' ],
  ['0006',' Six',' ',' ',' Thu Apr 12 20:24:43 GMT+08:00 2018',' H.O.A. dues for month of April',' HOA Dues',' ',' 400.0',' ',' ',' ',' ','' ]
]

let filtered = arr.filter(e => e[0] == '0004')

console.log(filtered)




答案 1 :(得分:0)

假设你的数组是有效的(带有值的引号),你可以这样简单地过滤它:



const bigArray = [
	["Account No.", "Acc. Name", "O/R, Ack. Receipt", "Date", "Description", "Category", "Credit", "Debit", "P. Method", "Chq. Date", "Chq. No.", "Bank Name", "Deposited"],
	["0001", "Snow", , , "Thu Apr 12 20:24:41 GMT+08:00 2018", "H.O.A. dues for month of April","HOA Dues", , "400.0", , , , , ],
	["0002", "Two", , , "Thu Apr 12 20:24:41 GMT+08:00 2018", "H.O.A. dues for month of April", "HOA Dues", , "400.0", , , , , ],
	["0003", "Three", , , "Thu Apr 12 20:24:41 GMT+08:00 2018", "H.O.A. dues for month of April", "HOA Dues", , "400.0", , , , , ]
]

let filtered = bigArray.filter( subArray => subArray.includes("0002") )

console.log(filtered)