我在网站上使用的插件会以这种方式生成一个数组:
<script type="text/javascript">
window.slideDetails = [
//Slide1
"C1BP7: user information",
"FJAD7: user information",
"FFAD7: user information",
//Slide2
"C0AE7: user information",
"C7AZ7: user information",
"FJAE7: user information",
//Slide3
"C1AW7: user information",
"FJAP7: user information",
"FFAD7: user information"
];
</script>
我想使用jQuery cookie插件将前五个字符传递给cookie。是否可以将数组项截断为前五个字符?
我正试图绕过切片(),但我试过的不同代码根本不起作用。
编辑:
感谢andreas的回答,我稍微调整了一下:
window.slideCode = [], i;
for(var i = 0; i < window.slideDetails.length; i++) {
window.slideCode.push(window.slideDetails[i].substr(0, 5));
}
答案 0 :(得分:2)
for(var i = 0; i < window.slideDetails.length; i++) {
window.slideDetails[i] = window.slideDetails[i].substr(0, 5);
}