gpt4 book ai didi

javascript - 如何使用 jQuery 合并 2 个 json 文件中的数据

转载 作者:行者123 更新时间:2023-12-03 09:11:04 25 4
gpt4 key购买 nike

我正在尝试使用两个单独的 json 文件中的数据创建 Google map 。我尝试使用 jquery/javascript 组合这两个文件,然后通过 Google Maps API 处理生成的对象数组。

我尝试过 $.extend、$.merge、concat,甚至尝试将数据推送到空数组中,但在每种情况下,只有第一组数据出现在 map 上(尽管我可以如果我用 console.log 显示两组数据,请分别查看它们)。

我肯定做了一些根本性错误的事情,但我没有看到它。我的代码的相关部分如下(我尝试过的内容已被注释掉)。任何建议将不胜感激。

j$.when(
j$.getJSON('mapData1.js'),
j$.getJSON('mapData2.js')
).done(function(data1, data2) {
var d1 = data1;
var d2 = data2;
var d3 = [];
d3.push(d1[0]);
d3.push(d2[0]);
//var d3 = j$.extend({},d1,d2);
//var d3 = j$.merge(d1,d2);
//var d3 = d1.concat(d2);
var data = d3[0];

//code to process data with Google Maps API
});

我的 json 文件如下所示(但包含更多项目):

[
{
"ID": "a001a000002o4iZAAQ",
"NAME": "Atlanta",
"Address": "123 State Street",
"City": "Atlanta",
"StateAbbreviation": "GA",
"SF": "",
"LeaseExpiration": "8/31/2012",
"Occupancy": "2",
"Country": "USA",
"Address2": "",
"Lat": "33.7863317",
"Lng": "-84.3836873",
"Type": "loc",
"Color": "red"
}
]

最佳答案

您可以使用concat()

var array1 = [{
"ID-1": "a001a000002o4iZAAQ",
"NAME-1": "Atlanta",
"Address-1": "123 State Street",
"City-1": "Atlanta",
"StateAbbreviation-1": "GA",
"SF-1": "",
"LeaseExpiration-1": "8/31/2012",
"Occupancy-1": "2",
"Country-1": "USA",
"Address2-1": "",
"Lat-1": "33.7863317",
"Lng-1": "-84.3836873",
"Type-1": "loc",
"Color-1": "red"
}];
var array2 = [{
"ID-2": "a001a000002o4iZAAQ",
"NAME-2": "Atlanta",
"Address-2": "123 State Street",
"City-2": "Atlanta",
"StateAbbreviation-2": "GA",
"SF-2": "",
"LeaseExpiration-2": "8/31/2012",
"Occupancy-2": "2",
"Country-2": "USA",
"Address2-2": "",
"Lat-2": "33.7863317",
"Lng-2": "-84.3836873",
"Type-2": "loc",
"Color-2": "red"
}];

var array3 = array1.concat(array2);

alert(JSON.stringify(array3));

关于javascript - 如何使用 jQuery 合并 2 个 json 文件中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32071055/

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