【发布时间】:2022-03-12 01:51:50
【问题描述】:
let data = [{
system: {
id: "4gSSbjCFEorYXqrgDIP2FA",
type: "Entry",
name: "User"
},
DataDetails: {
shortOption: {
"en-us": "some value"
},
mediaFile: [{
sys: {
type: "Link",
link: "Entry",
id: "7kRzyt4PFo",
},
},
{
sys: {
type: "Link",
link: "Entry",
id: "2OspeCtNK0s",
},
},
],
mediaGalary: [{
sys: {
type: "Link",
link: "Asset",
id: "gHcw3Z1Ko",
},
},
{
sys: {
type: "Link",
linkType: "Asset",
id: "h2cPiuU9jIz",
},
},
],
mediaExplore: [{
sys: {
type: "Link",
link: "Asset",
id: "euwcbds3282",
},
},
{
sys: {
type: "Link",
linkType: "Asset",
id: "jndsiw23912",
},
},
],
singleMediaImage: {
sys: {
type: "Link",
linkType: "Asset",
id: "7kRzyt4PFo",
},
},
},
},
{
system: {
id: "1aBOO8tu3lUsjtICuIbUM5",
type: "Entry",
name: "User"
},
DataDetails: {
short: {
"en-us": "details of shorts"
},
shortSlugOption: {
"hi-In": "options"
},
booleanField: {
kl: "true"
},
},
},
{
system: {
id: "2pOUGnI1oRD7nsrYs600HA",
type: "Entry",
name: "Dummy"
},
DataDetails: {
testingNewValue: [{
sys: {
type: "Link",
link: "Entry",
id: "66rzYr2BpWL",
},
},
{
sys: {
type: "Link",
link: "Entry",
id: "1VTBHdLTdSW",
},
},
],
},
},
{
system: {
id: "66rzYr2BpWL1VTBHdLTdSW",
type: "Entry",
name: "new"
},
DataDetails: {
oneReference: {
sys: {
type: "Link",
linkType: "Asset",
id: "h2cPiuU9jIz",
},
},
multiReference: [{
sys: {
type: "Link",
link: "Asset",
id: "gHcw3Z1Ko",
},
},
{
sys: {
type: "Link",
link: "Asset",
id: "h2cPiuU9jIz",
},
},
],
},
},
{
system: {
id: "cIb5mqEBRWDD6hrNmFmFE",
type: "Entry",
name: "new"
},
DataDetails: {
testingNewValue: {
"hi-IN": "jksdsdo"
}
},
},
{
system: {
id: "7kRzyt4PFrX13gHcw3Z1Ko",
type: "Entry",
name: "Dummy"
},
DataDetails: {
testingNewValue: {
"en-us": "kknksdo"
}
},
},
];
let anotherObj = {
"gHcw3Z1Ko": {
status: true,
tag: [],
filename: "exute-image.jpg",
is_dir: false,
parent_uid: null,
},
"h2cPiuU9jIz": {
status: true,
tag: [],
filename: "wallpapers-6.jpg",
is_dir: false,
parent_uid: null,
},
"7kRzyt4PFo": {
status: true,
tag: [],
filename: "in-space-rk.jpg",
is_dir: false,
parent_uid: null,
},
"euwcbds3282": {
status: true,
tag: [],
filename: "justice-league.jpg",
is_dir: false,
parent_uid: null,
},
"jndsiw23912": {
status: true,
tag: [],
filename: "batman.jpg",
is_dir: false,
parent_uid: null,
},
};
let res = data.reduce((acc, curr) => {
if (!acc[curr.system.name]) {
acc[curr.system.name] = {};
}
let detailsObj = {};
let assetArray=[];
for (let key of Object.keys(curr.DataDetails)) {
detailsObj[key] = Object.values(curr.DataDetails[key])[0];
if (typeof Object.values(curr.DataDetails[key])[0] === "object") {
Object.values(curr.DataDetails[key]).map((type) => {
if (Array.isArray(type)) {
type.map((link) => {
if (link.sys.link === "Asset") {
for (let id in anotherObj) {
if (link.sys.id === id) {
assetArray.push(anotherObj[id]);
detailsObj[key] = assetArray;
}
}
}
});
} else {
if (type.sys !== undefined) {
// for single Asset
if (type.sys.link === "Asset") {
for (let id in anotherObj) {
if (type.sys.id === id) {
detailsObj[key] = [anotherObj[id]];
}
}
}
}
}
});
}
}
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);
我试图将两个对象数据放在一个输出中,但通过读取资产链接
但在多个数组列表中,输出显示为单个资产输出,因为 mediaFile 包含两个条目,但我不知道我在哪里做错了,它显示给我一个值
因为我的主要目的是用 anotherObj 关键数据替换 sys:{id:erfrdvsdf}
所以它应该是这样的,例如我的输出是这样的
"User": {
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/User/4gSSbjCFEorYXqrgDIP2FA",
"shortOption": "some value",
"mediaFile": {
"sys": {
"type": "Link",
"link": "Entry",
"id": "7kRzyt4PFo"
}
},
"mediaGalary": [
{
/**id:7**/
"status": true,
"tag": [],
"filename": "in-space-rk.jpg",
"is_dir": false,
"parent_uid": null
}
],
"singleMediaImage": {
"type": "Link",
"linkType": "Asset",
"id": "h2cPiuU9jIz"
}
},
},
但应该是这样的
"User": {
"4gSSbjCFEorYXqrgDIP2FA": {
"title": "4gSSbjCFEorYXqrgDIP2FA",
"uid": "4gSSbjCFEorYXqrgDIP2FA",
"url": "/User/4gSSbjCFEorYXqrgDIP2FA",
"shortOption": "some value",
"mediaFile": {
"sys": {
"type": "Link",
"link": "Entry",
"id": "7kRzyt4PFo"
}
},
"mediaGalary": [
{
"status": true,
"tag": [],
"filename": "in-space-rk.jpg",
"is_dir": false,
"parent_uid": null
},
{
"status": true,
"tag": [],
"filename": "wallpapers-6.jpg",
"is_dir": false,
"parent_uid": null
},
],
"mediaExplore": [
{
"status": true,
"tag": [],
"filename": "justice-league",
"is_dir": false,
"parent_uid": null
},
{
"status": true,
"tag": [],
"filename": "batman.jpg",
"is_dir": false,
"parent_uid": null
},
],
"singleMediaImage": [
{
"status": true,
"tag": [],
"filename": "wallpapers-6.jpg",
"is_dir": false,
"parent_uid": null
},
]
},
},
但我的预期输出是我所期待的
因此,我被输入字段卡住了,因为我在输入字段中遇到了同样的问题,因为我编写了相同的代码来获取数组中的输入字段和单个输入字段
我不知道我的代码在哪里中断
我知道这是很长的代码,但请帮我解决这个错误,因为我没有继续前进
【问题讨论】:
标签: javascript node.js arrays if-statement nodes