javascript 拆分字符串然后访问数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3864826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting a string then accessing array
提问by theorise
New to Javascript and jQuery, so I assume this is a pretty simple question.
Javascript 和 jQuery 的新手,所以我认为这是一个非常简单的问题。
I have a date I want to split into a string, then print out the array.
我有一个日期,我想将其拆分为一个字符串,然后打印出该数组。
var date = "12/10/2010";
var dateArray = date.split(/);
$('#printbox').html($(dateArray[0]));
<span id="printbox"></span>
<span id="printbox"></span>
Edit: The question is, how can I split "12/10/2010" into an array like this:
编辑:问题是,如何将“12/10/2010”拆分为这样的数组:
Array[0] = 12
Array[1] = 10
Array[3] = 2010
And how do I print them to HTML?
以及如何将它们打印为 HTML?
回答by Oded
The first parameter to the string.splitfunction should be a string or regex:
string.split函数的第一个参数应该是字符串或正则表达式:
var date = "12/10/2010";
var dateArray = date.split("/");
// dateArray[0] == 12
// dateArray[1] == 10
// dateArray[2] == 2010
// Now use the array to print out whichever way you want (jQuery.html(values))...
回答by Spilarix
var date = "12/10/2010";
var dateArray = date.split('/');
$('#printbox').html(dateArray[0]); // it displays 12 in printbox
$('#printbox').html(dateArray[1]); // it displays 10 in printbox
$('#printbox').html(dateArray[2]); // it displays 2010 in printbox
回答by willcodejavaforfood
For splitting you were almost there :)
对于分裂,你几乎就在那里:)
var dateArray = date.split("/");
If you want to use jQuery you could use the eachmethod.
如果你想使用 jQuery,你可以使用each方法。
$.each(dateArray, function(index, value) {
// print out here
});
This constructs a loop that iterates over the elements in your array. You can access this element by the valuevariable.
这构造了一个循环,循环访问数组中的元素。您可以通过value变量访问此元素。
回答by xj9
var date = "12/10/2010";
var dateArray = date.split('/');
// dateArray => ['12', '10', '2010']
If you want to use jQuery there is an each function that you can use:
如果你想使用 jQuery,你可以使用每个函数:
$.each(dateArray, function (index, value) {
$('#printbox').append(value);
});
There is also a .forEach method in newer browsers:
在较新的浏览器中还有一个 .forEach 方法:
dateArray.forEach(function (value) {
document.getElementById('printbox').
appendChild(document.createTextNode(value));
});
Older browsers can have this method via the Array.prototype:
较旧的浏览器可以通过 Array.prototype 使用此方法:
if (!(Array.prototype.forEach === 'function')) {
Array.prototype.forEach = function (fn) {
for (var i = 0, l = this.length; i < l; i += 1) {
fn(this[i]);
}
};
}
回答by Bryan Field
var date = "12/10/2010";
var dateArray = date.split(/\//); // have to quote regular expressions with /
document.write; // this does nothing
$('#printbox').html(dateArray[2] + '-' + dateArray[0] + '-' + dateArray[1]); // do what you want here. The code I provided should print 2010-12-10
回答by BenWells
var date = "12/10/2010";
var dateArray = date.split('/');
var year = dateArray.splice(2,1);
dateArray[3] = year;
should give you
应该给你
Array[0] = 12
Array[1] = 10
Array[3] = 2010
But why you need the year in the fourth key I don't know. (also removes from the third so you don't end up with two years)
但是我不知道为什么你需要第四个键中的年份。(也从第三个中删除,这样你就不会在两年内结束)

