我试图通过使用CoffeeScript拆分逗号将字符串拆分为数组元素。
stringArray = string.split(',')
for element in stringArray
console.log element
我遇到的问题是,在迭代数组时,某些组件没有得到控制台记录。我期望输出9个子串,因为有8个逗号。
相反,输出5个元素,而不输出4个元素。它们不会附加到其他元素或类似的东西上,只是缺失。
这是我试图解析的字符串,粗体元素是行为不端的元素:
A | downstream_gene_variant | MODIFIER | PMF1-BGLAP | ENSG00000260238 |文稿| ENST00000490491 | protein_coding ||||||||||| 134 | 1 || SNV | HGNC | HGNC:42953 ||| U3KQ54 | UPI000046FD09 | |||,A | non_coding_transcript_exon_variant | MODIFIER | PAQR6 | ENSG00000160781 |文稿| ENST00000491107 | processed_transcript | 7/7 || ENST00000491107.5:n.1738C>吨|| 1738 ||||||| -1 || SNV | HGNC | HGNC:30132 ||||||||,A | non_coding_transcript_exon_variant | MODIFIER | PAQR6 | ENSG00000160781 |文稿| ENST00000492619 | processed_transcript | 7/7 || ENST00000492619.5:n.1978C>吨|| ||| 1978年|||| -1 || SNV | HGNC | HGNC:30132 ||||||||,A | downstream_gene_variant | MODIFIER | PMF1 | ENSG00000160783 |成绩单| ENST00000497069 | nonsense_mediated_decay ||||||||||| 3748 | 1 | cds_start_NF | SNV | HGNC | HGNC:9112 ||| V9GYH6 | |||| UPI0003B92863,的 A | 3_prime_UTR_variant | MODIFIER | PAQR6 | ENSG00000160781 |文稿| ENST00000540423 | protein_coding | 5/5 || ENST00000540423。 5:C * 395C>吨|| 1584 ||||||| -1 || SNV | HGNC | HGNC:30132 ||| Q7Z4Q8 | |||| UPI00001B3F57 下,A | downstream_gene_variant | MODIFIER | PMF1-BGL AP | ENSG00000260238 |文稿| ENST00000567140 | protein_coding ||||||||||| 252 | 1 || SNV | HGNC | HGNC:42953 ||| A0A087WT04 | |||| UPI0001ED050B,的 A | 3_prime_UTR_variant | MODIFIER | PAQR6 | ENSG00000160781 |文稿| ENST00000612424 | protein_coding | 6/6 || ENST00000612424.4:C * 395C>吨|| 1892 ||||||| -1 || SNV | HGNC | HGNC:30132 || | B4DJ42 | |||| UPI00017A70F0 下,的 A | 3_prime_UTR_variant | MODIFIER | PAQR6 | ENSG00000160781 |文稿| ENST00000613336 | protein_coding | 5/5 || ENST00000613336.4:C * 395C>吨|| 1454 ||||||| -1 || SNV | HGNC | HGNC:30132 ||| Q7Z4Q8 | |||| UPI00001B3F57 下,的 A | 3_prime_UTR_variant | MODIFIER | PAQR6 | ENSG00000160781 |文稿| ENST00000623241 | 。protein_coding | 5/5 || ENST00000623241.3:C * 395C>吨|| 1816 ||||||| -1 || SNV | HGNC | HGNC:30132 ||| Q7Z4Q8 | |||| UPI00001B3F57
非常感谢任何输入!
解决
原来这是一个下游问题。我有if语句使用'return',认为它会继续迭代,而它实际上只是终止它。用'continue'替换那些'return'语句并且它有效。感谢所有关于问题和问题格式的输入!
答案 0 :(得分:1)
您的代码按原样运行。这是编译好的JS:
var element, i, len, string, stringArray;
string = "A|downstream_gene_variant|MODIFIER|PMF1-BGLAP|ENSG00000260238|Transcript|ENST00000490491|protein_coding|||||||||||134|1||SNV|HGNC|HGNC:42953|||U3KQ54|UPI000046FD09||||,A|non_coding_transcript_exon_variant|MODIFIER|PAQR6|ENSG00000160781|Transcript|ENST00000491107|processed_transcript|7/7||ENST00000491107.5:n.1738C>T||1738|||||||-1||SNV|HGNC|HGNC:30132||||||||,A|non_coding_transcript_exon_variant|MODIFIER|PAQR6|ENSG00000160781|Transcript|ENST00000492619|processed_transcript|7/7||ENST00000492619.5:n.1978C>T||1978|||||||-1||SNV|HGNC|HGNC:30132||||||||,A|downstream_gene_variant|MODIFIER|PMF1|ENSG00000160783|Transcript|ENST00000497069|nonsense_mediated_decay|||||||||||3748|1|cds_start_NF|SNV|HGNC|HGNC:9112|||V9GYH6|UPI0003B92863||||,A|3_prime_UTR_variant|MODIFIER|PAQR6|ENSG00000160781|Transcript|ENST00000540423|protein_coding|5/5||ENST00000540423.5:c.*395C>T||1584|||||||-1||SNV|HGNC|HGNC:30132|||Q7Z4Q8|UPI00001B3F57||||,A|downstream_gene_variant|MODIFIER|PMF1-BGLAP|ENSG00000260238|Transcript|ENST00000567140|protein_coding|||||||||||252|1||SNV|HGNC|HGNC:42953|||A0A087WT04|UPI0001ED050B||||,A|3_prime_UTR_variant|MODIFIER|PAQR6|ENSG00000160781|Transcript|ENST00000612424|protein_coding|6/6||ENST00000612424.4:c.*395C>T||1892|||||||-1||SNV|HGNC|HGNC:30132|||B4DJ42|UPI00017A70F0||||,A|3_prime_UTR_variant|MODIFIER|PAQR6|ENSG00000160781|Transcript|ENST00000613336|protein_coding|5/5||ENST00000613336.4:c.*395C>T||1454|||||||-1||SNV|HGNC|HGNC:30132|||Q7Z4Q8|UPI00001B3F57||||,A|3_prime_UTR_variant|MODIFIER|PAQR6|ENSG00000160781|Transcript|ENST00000623241|protein_coding|5/5||ENST00000623241.3:c.*395C>T||1816|||||||-1||SNV|HGNC|HGNC:30132|||Q7Z4Q8|UPI00001B3F57||||";
stringArray = string.split(',');
for (i = 0, len = stringArray.length; i < len; i++) {
element = stringArray[i];
console.log(element);
}
我只是将您的代码投入CoffeeScript.org's "Try It" feature。