gpt4 book ai didi

javascript - 将 JavaScript 对象添加到另一个对象的末尾时出现问题

转载 作者:行者123 更新时间:2023-12-02 15:21:18 26 4
gpt4 key购买 nike

我有一个函数,它从先前的 JSON 响应接收数据,调用另一个 Web 服务,并将第二次调用的结果添加到原始 JSON 响应的末尾。但是,当我使用扩展时,原始数组中的项目会被新数组中的项目替换。我知道使用 $.extend 会使用相同的键覆盖我的项目,但我找不到另一个选项,因为我没有尝试 $.merge 我需要的项目将它们连接起来。还有其他可行的选择吗?谢谢!

//data is the object to which I am trying to add data
function verificaFileServer(data, subprograma, etapa, destino, lista, listaFileServer){

//checking whether some variable has been passed into the function
if(listaFileServer !== undefined){

//SharePoint REST Endpoint
var urlREST = "/_api/lists/getbytitle('File Server')/items?" +
"$select=Title,Link,Modified&$orderby=Modified desc&$filter=IDdoEvento eq " + subprograma + " and EtapaPAS eq '" + etapa + "' and TipodeArquivo eq '" + listaFileServer + "'";

$.ajax({
url: urlREST,
headers: { "Accept": "application/json; odata=verbose"},
success:function(dadosFileServer) {

//trying to add to the end of my original JSON object
$.extend(data.d.results,dadosFileServer.d.results);

//creating a new array containing the original and the new data
dadosConcatenados = data.d.results.sort(function(a, b) {
return (b["Modified"] > a["Modified"]) ? 1 : ((b["Modified"] < a["Modified"]) ? -1 : 0);
});

//calling a function that will build the output based on the concatenated array
montaSaida(dadosConcatenados, subprograma, etapa, lista, destino);

}
});

} else {
//calling a function that will build the output based on the original data
montaSaida(data, subprograma, etapa, lista, destino);
}

}

最佳答案

However, the items on the original array get replaced by items in the new array.

尝试使用Array.prototype.concat()

var data = [1, 2, 3];

data = data.concat(4, 5, 6, 7, {"abc": [8, 9]});

console.log(data)

关于javascript - 将 JavaScript 对象添加到另一个对象的末尾时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com