2
我目前在学习Node.js,javascript等等。我来自C++。Javascript文本数组解析
我需要解析的阵列如:
======================================================================================================
No. Name Cask Current Latest Auto-Update State
======================================================================================================
1/38 5KPlayer 5kplayer latest latest
2/38 Adobe Photoshop CC adobe-photoshop-cc 16 16
3/38 Alfred alfred 3.3.1_806 3.3.2_818 Y ignored
4/38 AppCleaner appcleaner 3.4 3.4 Y ignored
5/38 Github Atom atom 1.15.0 1.15.0 Y ignored
6/38 BetterZipQL betterzipql latest latest
7/38 Boom boom 1.6,1490693621 1.6,1490693621
8/38 CheatSheet cheatsheet 1.2.7 1.2.7
9/38 Cyberduck cyberduck 5.4.0.23761 5.4.0.23761
10/38 Dropbox dropbox 21.4.25 latest Y ignored
这是安装了Mac上应用的列表,每个应用1行。
如果该应用程序已过期('current'!='latest'),我会保留该行并为其做后期处理。
我想出了一个肮脏的 - 可是working-解决方案:
function parseBrewCUArray(array) {
var toUpdate = [];
var lines = array.split('\n');
//remove useless lines
lines = lines.slice(3);
for (var i=0; i<lines.length; i++) {
splittedLine = lines[i].split(/[ ]{2,}/);
if (splittedLine[3] != splittedLine[4]) {
toUpdate.push(splittedLine)
console.log(splittedLine);
}
}
}
但是,必须有一个非常更好的解决方案在那里!有人可以优化这一点,使这段代码更美丽?
非常感谢这个答案,超出了我的预期。我非常喜欢JSON化的方法。 – deadbird