我有两个JSON对象数组 - cads
和uploads
:
{
"cad":[
{
"cad_id":246,
"call_operator":"controller",
"user_id":"136",
"date":"2017-12-12 20:44:30",
}
],
"uploads":[
{
"file_id":"1",
"cad_id":"246",
"file_category":"prf",
"file_name":"e9283220bacfe5ca5c6a7edadef37e83.pdf"
},
{
"file_id":"2",
"cad_id":"246",
"file_category":"radio_log",
"file_name":"62d72ee7f69e17b54848df082dd72323.pdf"
}
]
}
我想取uploads
数组并将其附加到cads
数组,以便它在cads
数组中,而不是有两个单独的数组。< / p>
{
"cad":[
{
"cad_id":246,
"call_operator":"controller",
"user_id":"136",
"date":"2017-12-12 20:44:30",
"uploads":[
{
"file_id":"1",
"cad_id":"246",
"file_category":"prf",
"file_name":"e9283220bacfe5ca5c6a7edadef37e83.pdf"
},
{
"file_id":"2",
"cad_id":"246",
"file_category":"radio_log",
"file_name":"62d72ee7f69e17b54848df082dd72323.pdf"
}
]
}
]
}
如果有帮助,cad
数组中只有1个元素。 uploads
数组可以包含多个项目。
我如何在JS(或jQuery)中执行此操作?
答案 0 :(得分:0)
只需将uploads
分配到您想要的地方即可。
data.cad[0].uploads = data.uploads;
这假定JSON已被解析为由data
变量持有的JS对象。
如果愿意,您可以删除原始uploads
属性。
delete data.uploads;
答案 1 :(得分:0)
一种方法 -
var json = {
"cad":[
{
"cad_id":246,
"call_operator":"controller",
"user_id":"136",
"date":"2017-12-12 20:44:30",
}
],
"uploads":[
{
"file_id":"1",
"cad_id":"246",
"file_category":"prf",
"file_name":"e9283220bacfe5ca5c6a7edadef37e83.pdf"
},
{
"file_id":"2",
"cad_id":"246",
"file_category":"radio_log",
"file_name":"62d72ee7f69e17b54848df082dd72323.pdf"
}
]
}
var newObj= {};
newObj.cad = json.cad;
newObj.cad[0].uploads= json.uploads;
console.log(newObj);
&#13;
答案 2 :(得分:0)
var myArray = {
"cad":[
{
"cad_id":246,
"call_operator":"controller",
"user_id":"136",
"date":"2017-12-12 20:44:30",
}
],
"uploads":[
{
"file_id":"1",
"cad_id":"246",
"file_category":"prf",
"file_name":"e9283220bacfe5ca5c6a7edadef37e83.pdf"
},
{
"file_id":"2",
"cad_id":"246",
"file_category":"radio_log",
"file_name":"62d72ee7f69e17b54848df082dd72323.pdf"
}
]
};
myArray.cad[0].uploads = myArray.uploads;
var newArray = myArray.cad;
console.log(newArray);
&#13;
答案 3 :(得分:0)
您可以使用reduce
按cad_id
组合数组:
let obj = {"cad":[{"cad_id":246,"call_operator":"controller","user_id":"136","date":"2017-12-12 20:44:30",}],"uploads":[{"file_id":"1","cad_id":"246","file_category":"prf","file_name":"e9283220bacfe5ca5c6a7edadef37e83.pdf"},{"file_id":"2","cad_id":"246","file_category":"radio_log","file_name":"62d72ee7f69e17b54848df082dd72323.pdf"}]};
obj = obj.uploads.reduce((a, b) => {
const e = a.find(e => e.cad_id == b.cad_id);
if (! e) return a; // change this to do something different with a not found cat_id
e.uploads = (e.uploads || []).concat(b);
return a;
}, obj.cad);
console.log(obj);
&#13;
请注意,如果cat
数组没有包含给定cat_id
的项目,则上述内容不会执行任何操作。例如,您可以更改该行为以创建新类别。
答案 4 :(得分:0)
遍历每次上传并找到相应的cad。如果该属性不存在,请创建该属性并将该上载添加到该属性。然后最后删除data.uploads
data.uploads.forEach(u => {
var cad = data.cad.find(c => +c.cad_id === +u.cad_id) ||{};
cad.uploads = (cad.uploads || []).concat(u);
});
delete data.uploads;
console.log(data)
<script>
var data = {
"cad": [{
"cad_id": 246,
"call_operator": "controller",
"user_id": "136",
"date": "2017-12-12 20:44:30",
}],
"uploads": [{
"file_id": "1",
"cad_id": "246",
"file_category": "prf",
"file_name": "e9283220bacfe5ca5c6a7edadef37e83.pdf"
},
{
"file_id": "2",
"cad_id": "246",
"file_category": "radio_log",
"file_name": "62d72ee7f69e17b54848df082dd72323.pdf"
}
]
}
</script>
答案 5 :(得分:0)
您可以使用哈希表来获取具有相同cad_id
的元素,并将上传对象推送到正确的数组。
分配后,从对象中删除属性upload
。
var data = { cad: [{ cad_id: 246, call_operator: "controller", user_id: "136", date: "2017-12-12 20:44:30" }], uploads: [{ file_id: "1", cad_id: "246", file_category: "prf", file_name: "e9283220bacfe5ca5c6a7edadef37e83.pdf" }, { file_id: "2", cad_id: "246", file_category: "radio_log", file_name: "62d72ee7f69e17b54848df082dd72323.pdf" }] },
hash = Object.create(null);
data.cad.forEach(function (o) {
hash[o.cad_id] = (o.uploads = []);
});
data.uploads.forEach(function (o) {
hash[o.cad_id].push(o);
});
delete data.uploads;
console.log(data);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;