拆分字符串然后访问数组

时间:2022-04-29 15:41:19

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>


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?

6 个解决方案

#1


9  

The first parameter to the string.split function 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))...

#2


2  

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

#3


1  

For splitting you were almost there :)

为了分裂你几乎在那里:)

var dateArray = date.split("/");

If you want to use jQuery you could use the each method.

如果你想使用jQuery,你可以使用每种方法。

$.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 value variable.

这构造了一个循环,迭代数组中的元素。您可以通过值变量访问此元素。

#4


1  

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]);
        }
    };
}

#5


0  

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

#6


0  

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)

但是为什么你需要第四把钥匙的年份我不知道。 (也从第三个中删除,所以你不会结束两年)

#1


9  

The first parameter to the string.split function 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))...

#2


2  

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

#3


1  

For splitting you were almost there :)

为了分裂你几乎在那里:)

var dateArray = date.split("/");

If you want to use jQuery you could use the each method.

如果你想使用jQuery,你可以使用每种方法。

$.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 value variable.

这构造了一个循环,迭代数组中的元素。您可以通过值变量访问此元素。

#4


1  

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]);
        }
    };
}

#5


0  

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

#6


0  

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)

但是为什么你需要第四把钥匙的年份我不知道。 (也从第三个中删除,所以你不会结束两年)