Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中将对象附加到子对象_Javascript_Jquery - Fatal编程技术网

在javascript中将对象附加到子对象

在javascript中将对象附加到子对象,javascript,jquery,Javascript,Jquery,我的目标如下: var dpsets = { "routes": [ { "name": "first", "values": [ "info", "more info", "text" ] }, { "name": "second",

我的目标如下:

var dpsets = {
    "routes": [
        {
            "name": "first",
            "values": [
                "info",
                "more info",
                "text"
            ]
        },
        {
            "name": "second",
            "values": [
                "text again",
                "with info"

            ]
        }
    ]
}
var currentSet = {
    "name": "New Route",
    "values": [
        "Latest one",
        "Latest two"
    ]
}
我还有以下目标:

var dpsets = {
    "routes": [
        {
            "name": "first",
            "values": [
                "info",
                "more info",
                "text"
            ]
        },
        {
            "name": "second",
            "values": [
                "text again",
                "with info"

            ]
        }
    ]
}
var currentSet = {
    "name": "New Route",
    "values": [
        "Latest one",
        "Latest two"
    ]
}
我试图将
currentSet
附加到
dpset
routes
元素

我已经尝试了
dpset.routes.push(currentSet)
和jQuery的
$.extend(dplocations.routes,currentRoute)
,但这两种方法似乎都没有达到我的要求

最好的方法是什么?

试试看-

dpsets.routes.push((currentSet));

alert(JSON.stringify(dpsets));

您是否尝试使用concat()方法


dpsets.concat(currentSet)

最初这不起作用:

dpset.routes.push(当前集)

然而,在再次尝试之后,它工作得很好

var dpsets = {
"routes": [
    {
        "name": "first",
        "values": [
            "info",
            "more info",
            "text"
        ]
    },
    {
        "name": "second",
        "values": [
            "text again",
            "with info"

        ]
    }
]
};
var currentSet = {
    "name": "New Route",
    "values": [
        "Latest one",
        "Latest two"
    ]
};

dpsets.routes.push(currentSet);
console.log(dpsets);

“dpset.routes.push(currentSet);”对我有效。检查此选项是否有效….-对于
dpsets.routes.push(currentSet)
,有什么不适合您?我刚刚再次尝试了dpsets.routes.push(currentSet),这次成功了。谢谢你的建议