Javascript如何将带有另一个数组的数组添加到新数组中?

时间:2019-10-28 11:35:24

标签: javascript arrays node.js

所以我现在的挑战是无法将具有另一个数组的数组添加到另一个新数组中。

我的代码:

var disabletimerange = { basedonservicetype: "", basedonservicer: "" }
var arr = [{"StartTime":"09:00:00","EndTime":"09:10:00"},{"StartTime":"10:00:00","EndTime":"15:00:00"}]
for (var xx = 0; xx < arr.length; xx++) {
    var bookedStartTime, bookedEndTime
    bookedStartTime = arr[xx].StartTime.slice(0, -3)
    bookedEndTime = arr[xx].EndTime.slice(0, -3)

    if (disabletimerange.basedonservicer === "") {
        var disabletimealreadybookedtiming = "[" + '"' + bookedStartTime + '"' + "," + '"' + bookedEndTime + '"' + "]"
        disabletimerange.basedonservicer = JSON.parse(disabletimealreadybookedtiming)
    } else {
        var disabletimealreadybookedtiming = "[" + '"' + bookedStartTime + '"' + "," + '"' + bookedEndTime + '"' + "]"
        disabletimerange.basedonservicer = "[" + "[" + disabletimerange.basedonservicer + "]" + "," + disabletimealreadybookedtiming + "]"
        disabletimerange.basedonservicer = JSON.parse(disabletimerange.basedonservicer)
}

未捕获到的SyntaxError:JSON中位于位置3->无法解析的最后一行的意外数字。

简而言之,我想要的基本上是2阵列棒tgt而不是添加到其中

var y = ["hi", "123"]
var x = ["yo", "312"]

this
[["hi", "123"], ["yo", "312"]] instead of ["hi", "123", "yo", "312"]

,它必须是数组。不是文本形式

2 个答案:

答案 0 :(得分:5)

您可以这样做:

var y = ["hi", "123"]
var x = ["yo", "312"]

var result = [x, y]

console.log(result)

答案 1 :(得分:0)

如您所描述

  

如何将带有另一个数组的数组添加到新数组中?

  • 创建一个新数组

  • 将您的数组一个接一个地推入这个新数组

let x = [1,2];
let y = [3.4];

let z = [];
z.push(x)
z.push(y);

console.log(z);