Javascript .map()不返回更新的数组

时间:2019-02-21 08:48:39

标签: javascript arrays

嗨,我有一个日期示例 public static void main(String[] args) { Observable.just("Alpha", "Beta", "Gamma" ,"Delta", "Epsilon") .delay(3000, TimeUnit.SECONDS) .subscribe(s -> System.out.println("Received: " + s)); sleep(3000); } public static void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } 的数组,我想将日期转换成所需的格式[2019-02-21T04:06:32.000Z]。所以我正在使用javascript地图

[02/21/2019 4:06:32 AM]

在此之后,我控制台了日期数组,但是它仍然显示dateArray.map( x => { return moment.tz(x, 'Etc/UTC').format('MM/DD/YYYY h:mm:ss A').toString() }); ,但是在map()内部,它以所需的格式显示。我在这里做错了什么?谁能帮我解决这个问题?谢谢。

2 个答案:

答案 0 :(得分:6)

structure(list(Year = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c("2001", "2008", "2018"), class = "factor"), Block = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "X"), class = "factor"), Plot = structure(1:20, .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c(" 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46"), class = "factor"), Agrimonia.eupatoria = structure(c(3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c("0", "1", "2", "6"), class = "factor"), Agrostis.capillaris = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c("0", "2", "3", "4", "6"), class = "factor"), Agrostis.gigantea = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 2L, 1L, 3L, 3L, 4L, 3L, 1L, 1L), .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c("0", "2", "3", "4", "6"), class = "factor"), Agrostis.stolonifera = structure(c(5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), .Label = c("0", "1", "18", "2", "3", "38", "4", "6", "68", "8"), class = "factor")), .Names = c("Year", "Block", "Plot", "Agrimonia.eupatoria", "Agrostis.capillaris", "Agrostis.gigantea", "Agrostis.stolonifera"), row.names = c("18A01", "18A02", "18A03", "18A04", "18A05", "18A06", "18A07", "18A08", "18A09", "18A10", "18A11", "18A12", "18A13", "18A14", "18A15", "18A16", "18A17", "18A18", "18A19", "18A20"), class = "data.frame") 没有使现有数组发生变异。 您应该这样做:

.map

答案 1 :(得分:0)

如果必须更改现有数组,请使用for循环。

dateArray = ['2019-02-21T04:06:32.000Z'];

for(let idx = 0; idx < dateArray.length; idx++){
  dateArray[idx] = moment.tz(dateArray[idx], 'Etc/UTC')
    .format('MM/DD/YYYY h:mm:ss A').toString();
}