admin管理员组文章数量:1426021
What's the best way to parse:
[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]
[ '[email protected]' ]
[ 'User Name <[email protected]>' ]
[ 'oi' ]
And take the [' '] out ?
Thanks
More details:
It's the heads of an IMAP e-mail.
msg.headers.date
returns the data, etc.
What I want is to have:
"Tue, 5 Apr 2011 15:15:59 +0100"
"[email protected]"
"User Name"
"[email protected]"
"oi"
What's the best way to parse:
[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]
[ '[email protected]' ]
[ 'User Name <[email protected]>' ]
[ 'oi' ]
And take the [' '] out ?
Thanks
More details:
It's the heads of an IMAP e-mail.
msg.headers.date
returns the data, etc.
What I want is to have:
"Tue, 5 Apr 2011 15:15:59 +0100"
"[email protected]"
"User Name"
"[email protected]"
"oi"
Share
Improve this question
edited Apr 5, 2011 at 21:40
donald
asked Apr 5, 2011 at 21:28
donalddonald
23.8k45 gold badges145 silver badges224 bronze badges
0
2 Answers
Reset to default 3So you're saying that console.log(msg.headers.date)
gives you [ 'Tue, 5 Apr 2011 15:15:59 +0100' ]
??
In that case, console.log(msg.headers.date[0])
== Tue, 5 Apr 2011 15:15:59 +0100
Is that what you're trying to get?
What is this? A file? Straight text? Part of a larger JSON structure?
Basically, convert it into an actual structure and load it, one way or another:
module.exports = [
[ 'Tue, 5 Apr 2011 15:15:59 +0100' ],
[ '[email protected]' ],
[ 'User Name <[email protected]>' ],
[ 'oi' ]
];
----
var info = require('./file');
// info[0][0] == Tue, 5 Apr 2011 15:15:59 +0100
or if you want to parse it:
var lines = [
"[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]",
"[ '[email protected]' ]",
"[ 'User Name <[email protected]>' ]",
"[ 'oi' ]"
];
var info = JSON.parse('[' + lines.join(',') + ']');
// info[0][0] == Tue, 5 Apr 2011 15:15:59 +0100
Assuming each line is an element in the array lines
:
var lines = [
"[ 'Tue, 5 Apr 2011 15:15:59 +0100' ]",
"[ '[email protected]' ]",
"[ 'User Name <[email protected]>' ]",
"[ 'oi' ]"
];
for(var i=0;i<lines.length;i++){
lines[i]=lines[i].replace(/^\[ *'|' *\]$/g,'');
}
console.log(JSON.stringify(lines));
本文标签: javascriptHow to Parse an Array in NodejsStack Overflow
版权声明:本文标题:javascript - How to Parse an Array in Node.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745396507a2656839.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论