我想在打字稿中以JSON添加两个嵌套对象。
在下面给出的JSON中,我想在第一个JSON的activityLogs中添加第二个JSON的activityLogs项。
JSON1:
[{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
{"gpsdate":"01/03/2019","gpstime":"13:38:18"},
{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
}]
JSON2:
[{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},
{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]
}]
结果:
[{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},
{"gpsdate":"01/03/2019","gpstime":"13:43:18"},
{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
{"gpsdate":"01/03/2019","gpstime":"13:38:18"},
{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
}]
我该怎么做?
答案 0 :(得分:0)
您可以使用隐式数组方法。
julia> s = """-5.871153289887625082e-01,8.095032986136727615e-01,9.900000000000006573e-01,9.900000000000006573e-01,-9.866066418838319585e-01,-3.138907529596844714e+00,-5.129218106887371675e+00,-4.803915800699462224e+00"""
"-5.871153289887625082e-01,8.095032986136727615e-01,9.900000000000006573e-01,9.900000000000006573e-01,-9.866066418838319585e-01,-3.138907529596844714e+00,-5.129218106887371675e+00,-4.803915800699462224e+00"
julia> CSV.read(IOBuffer(s), header=false)
1×8 DataFrame
│ Row │ Column1 │ Column2 │ Column3 │ Column4 │ Column5 │ Column6 │ Column7 │ Column8 │
│ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │ Float64⍰ │
├─────┼───────────┼──────────┼──────────┼──────────┼───────────┼──────────┼──────────┼──────────┤
│ 1 │ -0.587115 │ 0.809503 │ 0.99 │ 0.99 │ -0.986607 │ -3.13891 │ -5.12922 │ -4.80392 │
答案 1 :(得分:0)
您可以将push()
与点差运算符一起使用,或将concat
与重新分配符一起使用:
var JSON1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},{"gpsdate":"01/03/2019","gpstime":"13:38:18"},{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]}]
var JSON2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]}]
JSON1[0].activityLogs.push(...JSON2[0].activityLogs)
console.log(JSON1)
这假设您的json数组仅包含一个顶级对象。如果不是这种情况,则需要添加有关两个数组如何同步的更多详细信息(例如,vehicleno
是否会相同?)。
作为示例,如果vehicleno
是两个数组中的唯一标识符,则可以创建JSON1
值的查找以及将其推入适当的数组的用途。即使其中包含多辆载具,这也会在JSON1
位置进行更新:
var JSON1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},{"gpsdate":"01/03/2019","gpstime":"13:38:18"},{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]}]
var JSON2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]}]
let lookup = JSON1.reduce((lookup, obj) => {
lookup[obj.vehicleno] = obj
return lookup
}, {})
JSON2.forEach(obj => lookup[obj.vehicleno].activityLogs.push(...obj.activityLogs))
console.log(JSON1)
答案 2 :(得分:0)
最简单的方法是concat
activityLogs
:
var arr1 = [{
"vehicleno": "SV028",
"devicE_CODE": "8505",
"activityLogs": [{
"gpsdate": "01/03/2019",
"gpstime": "13:40:18"
},
{
"gpsdate": "01/03/2019",
"gpstime": "13:38:18"
},
{
"gpsdate": "01/03/2019",
"gpstime": "13:37:18"
}
]
}];
var arr2 = [{
"vehicleno": "SV028",
"devicE_CODE": "8505",
"activityLogs": [{
"gpsdate": "01/03/2019",
"gpstime": "13:46:18"
},
{
"gpsdate": "01/03/2019",
"gpstime": "13:43:18"
}
]
}];
var arr3 = arr1[0].activityLogs.concat(arr2[0].activityLogs);
console.log(arr3);
.as-console-wrapper {
max-height: 100% !important;
top: auto;
}
请注意,只有在顶层数组中只有一个对象时,此方法才起作用。
答案 3 :(得分:0)
result = json1;
/// result = Object.assign({}, json1); if you don't want to mutate the original json1
result.forEach(elem1 => elem1.activityLogs
.concat(json2.find(elem2 => elem2.vehicleno === elem1.vehicleno).activityLogs));
通过vehicleno
找到匹配的元素,将第二个数组项的activityLog与第一个数组项联系起来。
答案 4 :(得分:0)
var json1 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
{"gpsdate":"01/03/2019","gpstime":"13:38:18"},
{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
},{"vehicleno":"SV02","devicE_CODE":"8505","activityLogs":
[{"gpsdate":"01/03/2019","gpstime":"13:40:18"},
{"gpsdate":"01/03/2019","gpstime":"13:38:18"},
{"gpsdate":"01/03/2019","gpstime":"13:37:18"}]
}]
var json2 = [{"vehicleno":"SV028","devicE_CODE":"8505","activityLogs":
[{"gpsdate":"01/03/2019","gpstime":"13:46:18"},
{"gpsdate":"01/03/2019","gpstime":"13:43:18"}]
}];
var jsonCont = json1.concat(json2);
var result = Object.values(jsonCont.reduce((acc, o)=>{
if(!acc.hasOwnProperty(o['vehicleno'])) {
acc[o['vehicleno']] = Object.assign({}, o);
} else {
acc[o['vehicleno']]['activityLogs'] = acc[o['vehicleno']]['activityLogs'].concat(o['activityLogs']);
}
return acc;
}, {}));
console.log(result);