gpt4 book ai didi

javascript - 如何将级别添加到 JSON 数组?

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

我有一个包含 7,000 个对象的 JSON 数组,这些对象都在一个父对象下。我想在它们前面添加两个级别,一个是年份级别,然后是一个月级别。

目前我的结构如下:

{
//apod is single representation of main parent
"apod" : {
//below is a unique identifier with nested data
"-KgpW6owlA2YGwKFj2V-" : {
"date" : "1995-06-16",
"explanation" : "If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.",
"gsHdUrl" : "https://www.foo.com",
"gsThumbnailUrl" : "https://www.foo.com",
"hdurl" : "https://apod.nasa.gov/apod/image/e_lens.gif",
"media_type" : "image",
"service_version" : "v1",
"title" : "Neutron Star Earth",
"url" : "https://apod.nasa.gov/apod/image/e_lens.gif"
},
//below is another unique identifier with nested data
"-KgpW7fV9laX8YL30glD" : {
"date" : "1995-06-20",
"explanation" : "Today's Picture: June 20, 1995 The Pleiades Star Cluster Picture Credit: Mount Wilson Observatory Explanation: The Pleiades star cluster, M45, is one of the brightest star clusters visible in the northern hemisphere. It consists of many bright, hot stars that were all formed at the same time within a large cloud of interstellar dust and gas. The blue haze that accompanies them is due to very fine dust which still remains and preferentially reflects the blue light from the stars. We keep an archive of previous Astronomy Pictures of the Day. Astronomy Picture of the Day is brought to you by Robert Nemiroff and Jerry Bonnell . Original material on this page is copyrighted to Robert J. Nemiroff and Jerry T. Bonnell.",
"gsHdUrl" : "https://www.foo.com",
"gsThumbnailUrl" : "https://www.foo.com",
"hdurl" : "https://apod.nasa.gov/apod/image/pleiades2.gif",
"media_type" : "image",
"service_version" : "v1",
"title" : "Pleiades Star Cluster",
"url" : "https://apod.nasa.gov/apod/image/pleiades2.gif"
}
}

我想将其转换为如下所示:

{
"apod": {
"1995": {
"06": {
"-KgpW6owlA2YGwKFj2V-": {
"date": "1995-06-16",
"explanation": "If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star.",
"gsHdUrl": "https://www.foo.com",
"gsThumbnailUrl": "https://www.foo.com",
"hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
"media_type": "image",
"service_version": "v1",
"title": "Neutron Star Earth",
"url": "https://apod.nasa.gov/apod/image/e_lens.gif"
},
"-KgpW7fV9laX8YL30glD": {
"date": "1995-06-20",
"explanation": "Today's Picture: June 20, 1995 The Pleiades Star Cluster Picture Credit: Mount Wilson Observatory Explanation: The Pleiades star cluster, M45, is one of the brightest star clusters visible in the northern hemisphere. It consists of many bright, hot stars that were all formed at the same time within a large cloud of interstellar dust and gas. The blue haze that accompanies them is due to very fine dust which still remains and preferentially reflects the blue light from the stars. We keep an archive of previous Astronomy Pictures of the Day. Astronomy Picture of the Day is brought to you by Robert Nemiroff and Jerry Bonnell . Original material on this page is copyrighted to Robert J. Nemiroff and Jerry T. Bonnell.",
"gsHdUrl": "https://www.foo.com",
"gsThumbnailUrl": "https://www.foo.com",
"hdurl": "https://apod.nasa.gov/apod/image/pleiades2.gif",
"media_type": "image",
"service_version": "v1",
"title": "Pleiades Star Cluster",
"url": "https://apod.nasa.gov/apod/image/pleiades2.gif"
}
},
"07": {}
},
"1996": {}
}
}

我想保留key->object映射。循环并重组它的最佳方法是什么?我有点迷失,因为我没有太多地编辑/重组 JSON。

谢谢

最佳答案

你拥有的是对象,而不是数组。不管怎样,我已经注释掉了。

function convert( json ){

// `data` is a JavaScript object we get from parsing
// json. For simplicity we remove the `apod` property
// and add it prior to returning.
let data;
try {
data = JSON.parse(json).apod;
}
catch(e){
alert("invalid json!");
}

// Create a new object to store the mapped objects.
// We will convert this to a json string in the end.
const result = {};

// Iterate all properties.
const keys = Object.keys(data);
for( let key of keys ){

// Parse month and year.
const [year, month] = data[key].date.split("-", 2);

// If there hadn't been an occurrence of this year,
// introduce an object.
if( !result[year] )
result[year] = {};

// If there hadn't been an occurrence of this month
// in this year, introduce an object.
if( !result[year][month] )
result[year][month] = {};

// Add element to current year and month.
result[year][month][key] = data[key];
}

// Convert and return the mapped object as json.
return JSON.stringify({ apod: result });

}

关于javascript - 如何将级别添加到 JSON 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43498066/

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