【问题标题】:Different ways to push不同的推动方式
【发布时间】:2014-02-13 17:59:34
【问题描述】:

我有以下数组和三个变量:

array1 = ['']

    dateOutput = 1/1/14
    timeOutput = 12am
    tallysave = 100

我正在使用它来推送三个变量

array1.push(dateOutput + ', ' + timeOutput + tallysave)

但是如何将每个变量推入数组中,以便在推入时它们会像这样:(多维数组?)

array = [
        { "date": dateOutput, "time": timeOutput, },
    ];   

【问题讨论】:

标签: javascript arrays push


【解决方案1】:

您可以将数据作为对象推送:

array1.push({
   "date": dateOutput, 
   "time": timeOutput 
});   

【讨论】:

    【解决方案2】:
    array1.push({ "date": dateOutput, "time": timeOutput})
    

    array1.push([dateOutput, timeOutput])
    

    但在第二种方式中,您应该知道索引为 0 的 dateOutput 和索引为 1 的 timeOutput

    【讨论】:

    • 第二个是array1[0][0] 来获取dateOutput。
    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2013-02-25
    • 2021-12-15
    • 1970-01-01
    • 2013-08-25
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多