使用 javascript / node js 创建自定义 JSON 对象
creating custom JSON object by using javascript / node js
let data=
[
{
system: {
id: '4gSSbjCFEorYXqrgDIP2FA',
type: 'Entry',
name: `Author`
},
DataDetails: {
shortSlugOption: {
"en-us":`some value`
},
mediaFileAssetLink: {
"za-op":`file Linl`
},
mediaFileAssetGalary: {
"hi":`file link 2`
},
singleMediaImage: {
"li-sd-op":`file link single`
},
}
},
{
system: {
id: '1aBOO8tu3lUsjtICuIbUM5',
type: 'Entry',
name: `Author`
},
DataDetails: {
short: {
"en-us":`details of shorts`
},
shortSlugOption: {
"hi-In":`options`
},
booleanField: {
"kl":`true`
}
}
},
{
system: {
id: '2pOUGnI1oRD7nsrYs600HA',
type: 'Entry',
name: `testing`
},
DataDetails: { testingNewValue: {
"en-uk":`details value`
} }
},
{
system: {
id: '66rzYr2BpWL1VTBHdLTdSW',
type: 'Entry',
name: `new`
},
DataDetails: { oneReference: {
"ru-re":`values 1`
}, multiReference: {
"uk-es":`values 2`
} }
},
{
system: {
id: 'cIb5mqEBRWDD6hrNmFmFE',
type: 'Entry',
name: `new`
},
DataDetails: { testingNewValue: {
"hi-IN":`jksdsdo`
} }
},
{
system: {
id: '7kRzyt4PFrX13gHcw3Z1Ko',
type: 'Entry',
name: `testing`
},
DataDetails: { testingNewValue: {
"en-us":`kknksdo`
} }
},
{
system: {
id: '2OspeCtNK0sh2cPiuU9jIz',
type: 'Entry',
name: `dummy`
},
DataDetails: {
short: {
"gh-we":`dfvndkssa`
},
shortSlugOption: {
"jk-cv":`sdocjosmdc`
},
mediaFileAssetLink: {
"en-us":`jdsojocis`
},
booleanField: {
"cb-af":`true`
}
}
},
{
system: {
id: '2eAmIIuG4xkLvatkU3RUSy',
type: 'Entry',
name: `dummy`
},
DataDetails: { dummy: {
"en-us":`dshcifdvk`
}, india: {
"kh-vh-we":`sdci`
} }
},
{
system: {
id: '7hbdS3MgfZ73TOtlu1WfXw',
type: 'Entry',
name: `dummy`
},
DataDetails: { testingNewValue: {
"ax-lo":`sdcoklsdc`
}, locationField: {
"sd-op":`sdcndkdc`
} }
}
];
data.map((value)=>{
var name = value.system.id;
var details={};
details[name]={
title:value.system.id,
uid:value.system.id,
url:`/${value.system.name}/${value.system.id}`,
}
console.log(details)
})
我将我的数据存储在 JSON 文件中,但通过对当前对象进行一些修改。
我要存入JSON的数据是这样的
{
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/Author/4gSSbjCFEorYXqrgDIP2FA",
"shortSlugOption": "some value",
"mediaFileAssetLink": "file Linl"
},
"1aBOO8tu3lUsjtICuIbUM5": {
"title": "1aBOO8tu3lUsjtICuIbUM5",
"uid": "1aBOO8tu3lUsjtICuIbUM5",
"url": "/Author/1aBOO8tu3lUsjtICuIbUM5",
"short": "details of shorts",
"shortSlugOption": "options",
"booleanField": "true"
}
}
因此,为了从 Data 对象中获取条目,我创建了映射函数来获取值并获取我使用的 DataDetails 对象 Object.entries
for (const [key, value] of Object.entries(value.DataDetails)) {
console.log(`${key}: ${value}`);
}
所以我对代码进行了一些更改以获得所需的输出问题是我得到了这个
let data=
[
{
system: {
id: '4gSSbjCFEorYXqrgDIP2FA',
type: 'Entry',
name: `Author`
},
DataDetails: {
shortSlugOption: {
"en-us":`some value`
},
mediaFileAssetLink: {
"za-op":`file Linl`
},
mediaFileAssetGalary: {
"hi":`file link 2`
},
singleMediaImage: {
"li-sd-op":`file link single`
},
}
},
{
system: {
id: '1aBOO8tu3lUsjtICuIbUM5',
type: 'Entry',
name: `Author`
},
DataDetails: {
short: {
"en-us":`details of shorts`
},
shortSlugOption: {
"hi-In":`options`
},
booleanField: {
"kl":`true`
}
}
}
];
data.map((value)=>{
var name = value.system.id;
var details={};
for (const [key, values] of Object.entries(value.DataDetails)) {
details[name]={
title:value.system.id,
uid:value.system.id,
url:`/${value.system.name}/${value.system.id}`,
key:`${values}`
}
}
console.log(details)
})
我试图添加 ${key}
但它并没有让我这样做 我无法将 DataDetails 值作为我对象中的键
有什么可能的方法可以通过删除 en-us,za-op
n all
来获得 shortSlugOption, mediaFileAssetLink
的 DataDetails 值
因为我的预期值是因为名称字段在这个 JSON 对象中很常见所以我想将通用名称字段存储在一个 JSON 文件中
{
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/Author/4gSSbjCFEorYXqrgDIP2FA",
"shortSlugOption": "some value",
"mediaFileAssetLink": "file Linl"
},
"1aBOO8tu3lUsjtICuIbUM5": {
"title": "1aBOO8tu3lUsjtICuIbUM5",
"uid": "1aBOO8tu3lUsjtICuIbUM5",
"url": "/Author/1aBOO8tu3lUsjtICuIbUM5",
"short": "details of shorts",
"shortSlugOption": "options",
"booleanField": "true"
}
}
这就是您要找的吗?单独创建了details对象,下一步进行传播。我取了第一个也是唯一一个来自 Object.values
数组
的元素
let data=
[
{
system: {
id: '4gSSbjCFEorYXqrgDIP2FA',
type: 'Entry',
name: `Author`
},
DataDetails: {
shortSlugOption: {
"en-us":`some value`
},
mediaFileAssetLink: {
"za-op":`file Linl`
},
mediaFileAssetGalary: {
"hi":`file link 2`
},
singleMediaImage: {
"li-sd-op":`file link single`
},
}
}
];
data.map((value)=>{
var name = value.system.id;
var details={};
for (const [key, values] of Object.entries(value.DataDetails)) {
let detailsObj = {}
Object.keys(value.DataDetails).forEach((detail) => {
detailsObj[detail] = Object.values(value.DataDetails[detail])[0]
})
details[name]={
title:value.system.id,
uid:value.system.id,
url:`/${value.system.name}/${value.system.id}`,
...detailsObj
}
}
console.log(details)
})
要按 name
分组可以使用 Array.reduce
let data = [{"system":{"id":"4gSSbjCFEorYXqrgDIP2FA","type":"Entry","name":"Author"},"DataDetails":{"shortSlugOption":{"en-us":"some value"},"mediaFileAssetLink":{"za-op":"file Linl"},"mediaFileAssetGalary":{"hi":"file link 2"},"singleMediaImage":{"li-sd-op":"file link single"}}},{"system":{"id":"1aBOO8tu3lUsjtICuIbUM5","type":"Entry","name":"Author"},"DataDetails":{"short":{"en-us":"details of shorts"},"shortSlugOption":{"hi-In":"options"},"booleanField":{"kl":"true"}}},{"system":{"id":"2pOUGnI1oRD7nsrYs600HA","type":"Entry","name":"testing"},"DataDetails":{"testingNewValue":{"en-uk":"details value"}}},{"system":{"id":"66rzYr2BpWL1VTBHdLTdSW","type":"Entry","name":"new"},"DataDetails":{"oneReference":{"ru-re":"values 1"},"multiReference":{"uk-es":"values 2"}}},{"system":{"id":"cIb5mqEBRWDD6hrNmFmFE","type":"Entry","name":"new"},"DataDetails":{"testingNewValue":{"hi-IN":"jksdsdo"}}},{"system":{"id":"7kRzyt4PFrX13gHcw3Z1Ko","type":"Entry","name":"testing"},"DataDetails":{"testingNewValue":{"en-us":"kknksdo"}}},{"system":{"id":"2OspeCtNK0sh2cPiuU9jIz","type":"Entry","name":"dummy"},"DataDetails":{"short":{"gh-we":"dfvndkssa"},"shortSlugOption":{"jk-cv":"sdocjosmdc"},"mediaFileAssetLink":{"en-us":"jdsojocis"},"booleanField":{"cb-af":"true"}}},{"system":{"id":"2eAmIIuG4xkLvatkU3RUSy","type":"Entry","name":"dummy"},"DataDetails":{"dummy":{"en-us":"dshcifdvk"},"india":{"kh-vh-we":"sdci"}}},{"system":{"id":"7hbdS3MgfZ73TOtlu1WfXw","type":"Entry","name":"dummy"},"DataDetails":{"testingNewValue":{"ax-lo":"sdcoklsdc"},"locationField":{"sd-op":"sdcndkdc"}}}]
let res = data.reduce((acc,curr) => {
if(!acc[curr.system.name]){
acc[curr.system.name] = {}
}
let detailsObj = {}
Object.keys(curr.DataDetails).forEach((detail) => {
detailsObj[detail] = Object.values(curr.DataDetails[detail])[0]
})
acc[curr.system.name][curr.system.id] = {
title:curr.system.id,
uid:curr.system.id,
url:`/${curr.system.name}/${curr.system.id}`,
...detailsObj
}
return acc;
},{})
console.log(res)
可以访问特定名称的结果,例如res['Author']
let data=
[
{
system: {
id: '4gSSbjCFEorYXqrgDIP2FA',
type: 'Entry',
name: `Author`
},
DataDetails: {
shortSlugOption: {
"en-us":`some value`
},
mediaFileAssetLink: {
"za-op":`file Linl`
},
mediaFileAssetGalary: {
"hi":`file link 2`
},
singleMediaImage: {
"li-sd-op":`file link single`
},
}
},
{
system: {
id: '1aBOO8tu3lUsjtICuIbUM5',
type: 'Entry',
name: `Author`
},
DataDetails: {
short: {
"en-us":`details of shorts`
},
shortSlugOption: {
"hi-In":`options`
},
booleanField: {
"kl":`true`
}
}
},
{
system: {
id: '2pOUGnI1oRD7nsrYs600HA',
type: 'Entry',
name: `testing`
},
DataDetails: { testingNewValue: {
"en-uk":`details value`
} }
},
{
system: {
id: '66rzYr2BpWL1VTBHdLTdSW',
type: 'Entry',
name: `new`
},
DataDetails: { oneReference: {
"ru-re":`values 1`
}, multiReference: {
"uk-es":`values 2`
} }
},
{
system: {
id: 'cIb5mqEBRWDD6hrNmFmFE',
type: 'Entry',
name: `new`
},
DataDetails: { testingNewValue: {
"hi-IN":`jksdsdo`
} }
},
{
system: {
id: '7kRzyt4PFrX13gHcw3Z1Ko',
type: 'Entry',
name: `testing`
},
DataDetails: { testingNewValue: {
"en-us":`kknksdo`
} }
},
{
system: {
id: '2OspeCtNK0sh2cPiuU9jIz',
type: 'Entry',
name: `dummy`
},
DataDetails: {
short: {
"gh-we":`dfvndkssa`
},
shortSlugOption: {
"jk-cv":`sdocjosmdc`
},
mediaFileAssetLink: {
"en-us":`jdsojocis`
},
booleanField: {
"cb-af":`true`
}
}
},
{
system: {
id: '2eAmIIuG4xkLvatkU3RUSy',
type: 'Entry',
name: `dummy`
},
DataDetails: { dummy: {
"en-us":`dshcifdvk`
}, india: {
"kh-vh-we":`sdci`
} }
},
{
system: {
id: '7hbdS3MgfZ73TOtlu1WfXw',
type: 'Entry',
name: `dummy`
},
DataDetails: { testingNewValue: {
"ax-lo":`sdcoklsdc`
}, locationField: {
"sd-op":`sdcndkdc`
} }
}
];
data.map((value)=>{
var name = value.system.id;
var details={};
details[name]={
title:value.system.id,
uid:value.system.id,
url:`/${value.system.name}/${value.system.id}`,
}
console.log(details)
})
我将我的数据存储在 JSON 文件中,但通过对当前对象进行一些修改。
我要存入JSON的数据是这样的
{
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/Author/4gSSbjCFEorYXqrgDIP2FA",
"shortSlugOption": "some value",
"mediaFileAssetLink": "file Linl"
},
"1aBOO8tu3lUsjtICuIbUM5": {
"title": "1aBOO8tu3lUsjtICuIbUM5",
"uid": "1aBOO8tu3lUsjtICuIbUM5",
"url": "/Author/1aBOO8tu3lUsjtICuIbUM5",
"short": "details of shorts",
"shortSlugOption": "options",
"booleanField": "true"
}
}
因此,为了从 Data 对象中获取条目,我创建了映射函数来获取值并获取我使用的 DataDetails 对象 Object.entries
for (const [key, value] of Object.entries(value.DataDetails)) {
console.log(`${key}: ${value}`);
}
所以我对代码进行了一些更改以获得所需的输出问题是我得到了这个
let data=
[
{
system: {
id: '4gSSbjCFEorYXqrgDIP2FA',
type: 'Entry',
name: `Author`
},
DataDetails: {
shortSlugOption: {
"en-us":`some value`
},
mediaFileAssetLink: {
"za-op":`file Linl`
},
mediaFileAssetGalary: {
"hi":`file link 2`
},
singleMediaImage: {
"li-sd-op":`file link single`
},
}
},
{
system: {
id: '1aBOO8tu3lUsjtICuIbUM5',
type: 'Entry',
name: `Author`
},
DataDetails: {
short: {
"en-us":`details of shorts`
},
shortSlugOption: {
"hi-In":`options`
},
booleanField: {
"kl":`true`
}
}
}
];
data.map((value)=>{
var name = value.system.id;
var details={};
for (const [key, values] of Object.entries(value.DataDetails)) {
details[name]={
title:value.system.id,
uid:value.system.id,
url:`/${value.system.name}/${value.system.id}`,
key:`${values}`
}
}
console.log(details)
})
我试图添加 ${key}
但它并没有让我这样做 我无法将 DataDetails 值作为我对象中的键
有什么可能的方法可以通过删除 en-us,za-op
n all
shortSlugOption, mediaFileAssetLink
的 DataDetails 值
因为我的预期值是因为名称字段在这个 JSON 对象中很常见所以我想将通用名称字段存储在一个 JSON 文件中
{
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/Author/4gSSbjCFEorYXqrgDIP2FA",
"shortSlugOption": "some value",
"mediaFileAssetLink": "file Linl"
},
"1aBOO8tu3lUsjtICuIbUM5": {
"title": "1aBOO8tu3lUsjtICuIbUM5",
"uid": "1aBOO8tu3lUsjtICuIbUM5",
"url": "/Author/1aBOO8tu3lUsjtICuIbUM5",
"short": "details of shorts",
"shortSlugOption": "options",
"booleanField": "true"
}
}
这就是您要找的吗?单独创建了details对象,下一步进行传播。我取了第一个也是唯一一个来自 Object.values
数组
let data=
[
{
system: {
id: '4gSSbjCFEorYXqrgDIP2FA',
type: 'Entry',
name: `Author`
},
DataDetails: {
shortSlugOption: {
"en-us":`some value`
},
mediaFileAssetLink: {
"za-op":`file Linl`
},
mediaFileAssetGalary: {
"hi":`file link 2`
},
singleMediaImage: {
"li-sd-op":`file link single`
},
}
}
];
data.map((value)=>{
var name = value.system.id;
var details={};
for (const [key, values] of Object.entries(value.DataDetails)) {
let detailsObj = {}
Object.keys(value.DataDetails).forEach((detail) => {
detailsObj[detail] = Object.values(value.DataDetails[detail])[0]
})
details[name]={
title:value.system.id,
uid:value.system.id,
url:`/${value.system.name}/${value.system.id}`,
...detailsObj
}
}
console.log(details)
})
要按 name
分组可以使用 Array.reduce
let data = [{"system":{"id":"4gSSbjCFEorYXqrgDIP2FA","type":"Entry","name":"Author"},"DataDetails":{"shortSlugOption":{"en-us":"some value"},"mediaFileAssetLink":{"za-op":"file Linl"},"mediaFileAssetGalary":{"hi":"file link 2"},"singleMediaImage":{"li-sd-op":"file link single"}}},{"system":{"id":"1aBOO8tu3lUsjtICuIbUM5","type":"Entry","name":"Author"},"DataDetails":{"short":{"en-us":"details of shorts"},"shortSlugOption":{"hi-In":"options"},"booleanField":{"kl":"true"}}},{"system":{"id":"2pOUGnI1oRD7nsrYs600HA","type":"Entry","name":"testing"},"DataDetails":{"testingNewValue":{"en-uk":"details value"}}},{"system":{"id":"66rzYr2BpWL1VTBHdLTdSW","type":"Entry","name":"new"},"DataDetails":{"oneReference":{"ru-re":"values 1"},"multiReference":{"uk-es":"values 2"}}},{"system":{"id":"cIb5mqEBRWDD6hrNmFmFE","type":"Entry","name":"new"},"DataDetails":{"testingNewValue":{"hi-IN":"jksdsdo"}}},{"system":{"id":"7kRzyt4PFrX13gHcw3Z1Ko","type":"Entry","name":"testing"},"DataDetails":{"testingNewValue":{"en-us":"kknksdo"}}},{"system":{"id":"2OspeCtNK0sh2cPiuU9jIz","type":"Entry","name":"dummy"},"DataDetails":{"short":{"gh-we":"dfvndkssa"},"shortSlugOption":{"jk-cv":"sdocjosmdc"},"mediaFileAssetLink":{"en-us":"jdsojocis"},"booleanField":{"cb-af":"true"}}},{"system":{"id":"2eAmIIuG4xkLvatkU3RUSy","type":"Entry","name":"dummy"},"DataDetails":{"dummy":{"en-us":"dshcifdvk"},"india":{"kh-vh-we":"sdci"}}},{"system":{"id":"7hbdS3MgfZ73TOtlu1WfXw","type":"Entry","name":"dummy"},"DataDetails":{"testingNewValue":{"ax-lo":"sdcoklsdc"},"locationField":{"sd-op":"sdcndkdc"}}}]
let res = data.reduce((acc,curr) => {
if(!acc[curr.system.name]){
acc[curr.system.name] = {}
}
let detailsObj = {}
Object.keys(curr.DataDetails).forEach((detail) => {
detailsObj[detail] = Object.values(curr.DataDetails[detail])[0]
})
acc[curr.system.name][curr.system.id] = {
title:curr.system.id,
uid:curr.system.id,
url:`/${curr.system.name}/${curr.system.id}`,
...detailsObj
}
return acc;
},{})
console.log(res)
可以访问特定名称的结果,例如res['Author']