我有一个JSON文件,它有重复的密钥,我想将重复的密钥及其值合并到一条记录中。
示例:
Json看起来像这样:
{
"numbers": {
"0BJA10GH001": {
"links": [
{
"name": "RI-Fliessbild: RG MFB L3 RI_002",
"path": "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf",
"searchNumber": true
},
{
"name": "Katalog Sensor: TGM / siehe 0BJA10GH001 -1_5.1",
"path": "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf",
"searchNumber": false
},
{
"name": "Datenblatt: 0BJA10GH001",
"path": "/Datenblaetter Messstellenliste/RG MFB L3 RI_002/0BJA10GH001.pdf",
"searchNumber": false
}
],
"aliases": [
"0BJA10GH001"
]
},
"0BJA10GH001": {
"links": [
{
"name": "RI-Fliessbild: RG MFB L3 RI_002",
"path": "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf",
"searchNumber": true
},
{
"name": "Komponentenbeschreibung: TGM / ---",
"path": "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf",
"searchNumber": false
},
{
"name": "Datenblatt: 0BJA10GH001",
"path": "/Datenblaetter Apparateliste/RG MFB L3 RI_002/0BJA10GH001.pdf",
"searchNumber": false
}
],
"aliases": [
"0BJA10GH001"
]
}
} }
我希望它看起来像这样:
{
"numbers": {
"0BJA10GH001": {
"links": [
{
"name": "RI-Fliessbild: RG MFB L3 RI_002",
"path": "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf",
"searchNumber": true
},
{
"name": "Katalog Sensor: TGM / siehe 0BJA10GH001 -1_5.1",
"path": "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf",
"searchNumber": false
},
{
"name": "Datenblatt: 0BJA10GH001",
"path": "/Datenblaetter Messstellenliste/RG MFB L3 RI_002/0BJA10GH001.pdf"
},
{
"name": "Komponentenbeschreibung: TGM / ---",
"path": "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf",
"searchNumber": false
},
{
"name": "Datenblatt: 0BJA10GH001",
"path": "/Datenblaetter Apparateliste/RG MFB L3 RI_002/0BJA10GH001.pdf",
"searchNumber": false
}
],
"aliases": [
"0BJA10GH001"
]
}
}
}
两个键“0BJA10GH001”是相同的合并,里面的重复值被删除。
var SeatWithCat = [{"0BJA10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Katalog Sensor: TGM / siehe 0BJA10GH001 -1_5.1", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BJA10GH001", "path" : "/Datenblaetter Messstellenliste/RG MFB L3 RI_002/0BJA10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BJA10GH001"] },
"0BJA10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: TGM / ---", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BJA10GH001", "path" : "/Datenblaetter Apparateliste/RG MFB L3 RI_002/0BJA10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BJA10GH001"] },
"0BMC10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Katalog Sensor: TGM / siehe 0BMC10GH001 -1_5.1", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BMC10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BMC10GH001", "path" : "/Datenblaetter Messstellenliste/RG MFB L3 RI_002/0BMC10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BMC10GH001"] },
"0BMC10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: TGM / ---", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BMC10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BMC10GH001", "path" : "/Datenblaetter Apparateliste/RG MFB L3 RI_002/0BMC10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BMC10GH001"] },
"0CFB01BB020" : { "links" : [{ "name" : "RI-Fliessbild: 90034652", "path" : "/C1_RI-Fliessbilder/C1.1/1_2/90034652.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: Messer Gas / HCl in N2", "path" : "/1_Verfahrenstechnik/Teil B/1_B7/1_B7.5/01_0CFB01BB020.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0CFB01BB020", "path" : "/Datenblaetter Apparateliste/90034652/0CFB01BB020.pdf", "searchNumber" : false }], "aliases" : ["0CFB01BB020"] },
"0CFB01BB021" : { "links" : [{ "name" : "RI-Fliessbild: 90034652", "path" : "/C1_RI-Fliessbilder/C1.1/1_2/90034652.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: Messer Gas / NH3 in N2", "path" : "/1_Verfahrenstechnik/Teil B/1_B7/1_B7.5/01_0CFB01BB021.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0CFB01BB021", "path" : "/Datenblaetter Apparateliste/90034652/0CFB01BB021.pdf", "searchNumber" : false }], "aliases" : ["0CFB01BB021"] }}];
function combine(arr) {
var combined = arr.reduce(function(result, item) {
var current = result[item.key];
result[item.key] = !current ? item : {
kks: item.key,
};
return result;
}, {});
return Object.keys(combined).map(function(key) {
return combined[key.key];
});
}
var result = combine(SeatWithCat);
console.log(result);
document.write(JSON.stringify(result));
这是我的代码到目前为止,它适用于第一部分。它删除了双键“0BJA10GH001”,但数组中的值不会从重复键中加入。
每个键“0BJA10GH001”里面都有一个“链接”数组,当重复项合并时,这些“链接”数组应该相互连接。
重复键“0BJA10GH001”内的所有值都应加入一个数组。我怎样才能实现这一目标? 更进一步,我如何删除数组中的重复项?
非常感谢。
现在代码:
var SeatWithCat = [{"0BJA10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Katalog Sensor: TGM / siehe 0BJA10GH001 -1_5.1", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BJA10GH001", "path" : "/Datenblaetter Messstellenliste/RG MFB L3 RI_002/0BJA10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BJA10GH001"] },
"0BJA10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: TGM / ---", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BJA10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BJA10GH001", "path" : "/Datenblaetter Apparateliste/RG MFB L3 RI_002/0BJA10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BJA10GH001"] },
"0BMC10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Katalog Sensor: TGM / siehe 0BMC10GH001 -1_5.1", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BMC10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BMC10GH001", "path" : "/Datenblaetter Messstellenliste/RG MFB L3 RI_002/0BMC10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BMC10GH001"] },
"0BMC10GH001" : { "links" : [{ "name" : "RI-Fliessbild: RG MFB L3 RI_002", "path" : "/C1_RI-Fliessbilder/C1.1/3/RG MFB L3 RI_002.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: TGM / ---", "path" : "/1_Verfahrenstechnik/Teil B/1_B5/1_B5.1/01_0BMC10GH001.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0BMC10GH001", "path" : "/Datenblaetter Apparateliste/RG MFB L3 RI_002/0BMC10GH001.pdf", "searchNumber" : false }], "aliases" : ["0BMC10GH001"] },
"0CFB01BB020" : { "links" : [{ "name" : "RI-Fliessbild: 90034652", "path" : "/C1_RI-Fliessbilder/C1.1/1_2/90034652.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: Messer Gas / HCl in N2", "path" : "/1_Verfahrenstechnik/Teil B/1_B7/1_B7.5/01_0CFB01BB020.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0CFB01BB020", "path" : "/Datenblaetter Apparateliste/90034652/0CFB01BB020.pdf", "searchNumber" : false }], "aliases" : ["0CFB01BB020"] },
"0CFB01BB021" : { "links" : [{ "name" : "RI-Fliessbild: 90034652", "path" : "/C1_RI-Fliessbilder/C1.1/1_2/90034652.pdf", "searchNumber" : true },{ "name" : "Komponentenbeschreibung: Messer Gas / NH3 in N2", "path" : "/1_Verfahrenstechnik/Teil B/1_B7/1_B7.5/01_0CFB01BB021.pdf", "searchNumber" : false },{ "name" : "Datenblatt: 0CFB01BB021", "path" : "/Datenblaetter Apparateliste/90034652/0CFB01BB021.pdf", "searchNumber" : false }], "aliases" : ["0CFB01BB021"] }}];
function combine(arr) {
var combined = arr.reduce(function(result, item) {
var current = result[item.key];
result[item.key] = !current ? item : {
kks: item.key,
};
return result;
}, {});
return Object.keys(combined).map(function(key) {
return combined[key.key];
});
}
var result = combine(SeatWithCat);
console.log(result);
答案 0 :(得分:0)
查找重复密钥代码:
var data = yourJson.numbers;
var keys = Object.keys(data);
var sizeOfData = keys.length;
for (var i=0; i < sizeOfData-1; i++){
for (var j=i; j< sizeOfData-1; j++){
if(keys[i] == keys[j+1]){
// here i and j+1 have same key and need another algo for merge
}
}
}