用 javascript 嵌套对象中的值交换键
Swap key with value in javascript nested object
所以我有这个对象:
{
"images": [
{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [
{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x" : [12,345],
"y": "234"
},
"long": 123
}
}
}
}
}
]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
我想用值交换键,所以它看起来像这样:
"ASDV1-01.jpg": "key",
"image.jpg": "image_location",
"data": {
"CE44DBAC-59B2-4178-8392-0141FB2F58DF": "documentid",
"Feb 1 2018 12:05PM": "scandate",
"1": "F08",
"101076": "F10",
这是我的代码,但它不起作用。我一直在尝试使用特定的 javascript 函数,但我就是想不通。另外,我想将结果显示为 JSON 字符串。你能提供一些关于如何做到这一点的线索吗?
function swap(json){
var ret = {};
for(var key in json){
ret[json[key]] = key;
}
return ret;
}
var result = swap(data)
console.log(JSON.stringify(result, null, 2));
将您的函数更改为像这样递归我认为它会对您有所帮助
let data = {
"images": [{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x": [12, 345],
"y": "234"
},
"long": 123
}
}
}
}
}]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
let ret = {};
function swap(json) {
for (var key in json) {
if (typeof json[key] === "object") {
swap(json[key])
} else {
ret[json[key]] = key;
}
}
}
swap(data)
console.log(ret);
所以我有这个对象:
{
"images": [
{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [
{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x" : [12,345],
"y": "234"
},
"long": 123
}
}
}
}
}
]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
我想用值交换键,所以它看起来像这样:
"ASDV1-01.jpg": "key",
"image.jpg": "image_location",
"data": {
"CE44DBAC-59B2-4178-8392-0141FB2F58DF": "documentid",
"Feb 1 2018 12:05PM": "scandate",
"1": "F08",
"101076": "F10",
这是我的代码,但它不起作用。我一直在尝试使用特定的 javascript 函数,但我就是想不通。另外,我想将结果显示为 JSON 字符串。你能提供一些关于如何做到这一点的线索吗?
function swap(json){
var ret = {};
for(var key in json){
ret[json[key]] = key;
}
return ret;
}
var result = swap(data)
console.log(JSON.stringify(result, null, 2));
将您的函数更改为像这样递归我认为它会对您有所帮助
let data = {
"images": [{
"key": "ASDV1-01.jpg",
"image_location": "image1.jpg",
"data": {
"documentid": "CE44DBAC-59B2-4178-8392-0141FB2F58DF",
"scandate": "Feb 1 2018 12:05PM",
"F08": "1",
"F09": "",
"F10": "101076",
"F11": ""
},
"crops": {
"F08": {
"rectangle": {
"left": 690,
"top": 2111,
"width": 597,
"height": 121
}
},
"F09": {},
"F10": {
"rectangle": {
"left": 653,
"top": 821,
"width": 653,
"height": 243
}
},
"F11": {}
}
},
{
"key": "ASDV1-01.jpg",
"image_location": "image.png",
"crops": {
"F05": {
"rectangle": {
"left": 0,
"top": 808,
"width": 624,
"height": 243
}
}
},
"metadata": [{
"name": "colors",
"data": {
"dimensions": {
"width": 2000,
"height": 2600
},
"coordinates": {
"width": {
"x": {
"lat": 4,
"long": [12, 345]
},
"y": {
"lat": {
"x": [12, 345],
"y": "234"
},
"long": 123
}
}
}
}
}]
},
{
"key": "ASDV1-02.jpg",
"image_location": "image.png"
}
]
}
let ret = {};
function swap(json) {
for (var key in json) {
if (typeof json[key] === "object") {
swap(json[key])
} else {
ret[json[key]] = key;
}
}
}
swap(data)
console.log(ret);