如果它与使用 Node JS / JavaScript 的其他 ID 匹配,则替换为 ID



const data = [
{
system: {
id: "4gSSbjCFEorYXqrgDIP2FA",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
shortSlugOption: { "en-us": "some value", "za-op": "random value" },
mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" },
mediaFileAssetGalary: { hi: "file link 2" },
singleMediaImage: { hi: "file link single", "en-us": "english link" },
},
},
{
system: {
id: "1aBOO8tu3lUsjtICuIbUM5",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
short: { "en-us": ["shorts", "values"], "za-op": "short details" },
shortSlugOption: { "hi-In": "options" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2pOUGnI1oRD7nsrYs600HA",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "details value" } },
},
{
system: {
id: "66rzYr2BpWL1VTBHdLTdSW",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: {
oneReference: { hi: "values 1" },
multiReference: { "hi-In": "values 2" },
},
},
{
system: {
id: "cIb5mqEBRWDD6hrNmFmFE",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } },
},
{
system: {
id: "7kRzyt4PFrX13gHcw3Z1Ko",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "kknksdo" } },
},
{
system: {
id: "2OspeCtNK0sh2cPiuU9jIz",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
short: { "za-op": "dfvndkssa" },
shortSlugOption: { hi: "sdocjosmdc" },
mediaFileAssetLink: { "en-us": "jdsojocis" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2eAmIIuG4xkLvatkU3RUSy",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
dummy: { "en-us": "dshcifdvk" },
india: { "za-op": "sdci", hi: "hewd" },
},
},
{
system: {
id: "7hbdS3MgfZ73TOtlu1WfXw",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
testingNewValue: { "en-us": "sdcoklsdc" },
locationField: { hi: "sdcndkdc" },
},
},
];
result = data.reduce(
(
r,
{
system: {
id,
content: {
type: { name },
},
},
DataDetails,
}
) => {
r[name] ??= {};
Object.entries(DataDetails).forEach(([key, object]) => {
Object.entries(object).forEach(([loc, value]) => {
r[name][loc] ??= {};
r[name][loc][id] ??= {};
r[name][loc][id][key] = value; 
});
});
return r;
},
{}
);
console.log(result)

我的 这是我的对象

领域

{
"Author": { "fields": "shortSlugOption" },
"testing": { "fields": "mediaaccess" },
"dummy": { "fields": "fieldtest" },
"new": { "fields": "fieldtest" },
}

我正在尝试检查并分配字段对象中存在的值以及侧结果 obj 中存在的键并分配值

所以在字段对象中

"new": { "fields": "oneReference" }

我想检查new中存在的一个引用

"new": {
"hi": {
"66rzYr2BpWL1VTBHdLTdSW": {
"oneReference": "values 1",
"media": {
"conf": {
"type": "media",
"id": "01"
}
}
}
},
"hi-In": {
"66rzYr2BpWL1VTBHdLTdSW": {
"multiReference": "values 2"
}
},
"hi-IN": {
"cIb5mqEBRWDD6hrNmFmFE": {
"testingNewValue": "jksdsdo"
}
}
},

所以在这里我的期望输出是这样的

预期输出

"new": {
"hi": {
"66rzYr2BpWL1VTBHdLTdSW": {
"subject": "values 1"
"id": "66rzYr2BpWL1VTBHdLTdSW",
"address": "/new/values 1"
"oneReference": "values 1",
"media": {
"conf": {
"type": "media",
"id": "01"
}
}
}
},
"hi-In": {
"66rzYr2BpWL1VTBHdLTdSW": {
"subject": "unknown"
"id": "66rzYr2BpWL1VTBHdLTdSW",
"address": "/new/unknown"
"multiReference": "values 2"
}
},
"hi-IN": {
"cIb5mqEBRWDD6hrNmFmFE": {
"subject": "unknown"
"id": "cIb5mqEBRWDD6hrNmFmFE",
"address": "/new/unknown"
"testingNewValue": "jksdsdo"
}
}
},
"Author": {
"en-us": {
"4gSSbjCFEorYXqrgDIP2FA": {
"subject": "some value",
"id": "4gSSbjCFEorYXqrgDIP2FA",
"address": "Author/some value",
"shortSlugOption": "some value",
"mediaFileAssetLink": "some file",
"singleMediaImage": "english link"
}
},
"za-op": {
"4gSSbjCFEorYXqrgDIP2FA": {
"subject": "random value",
"id": "4gSSbjCFEorYXqrgDIP2FA",
"address": "Author/random value",
"shortSlugOption": "random value",
"mediaFileAssetLink": "file Linl"
}
},
"hi": {
"4gSSbjCFEorYXqrgDIP2FA": {
"subject": "unknown",
"id": "4gSSbjCFEorYXqrgDIP2FA",
"address": "Author/unknown",
"mediaFileAssetGalary": "file link 2",
"singleMediaImage": "file link single"
}
}
}

在这里我们可以看到new:hinew:hi-In包含相同的 id66rzYr2BpWL1VTBHdLTdSW因此它应该检查字段对象oneReference存在于哪个 id 中并在subject中分配该值,但在 IDcIb5mqEBRWDD6hrNmFmFE不包含oneReferencenew:hi-IN中,那么它应该打印这样的unknown我想检查Author testing dummy以及更多,如果它们存在于fields对象中,并且检查result中是否存在值,如果该值不存在,则显示"未知",但显示实际键值

所以我试过这个但没有帮助

let field = {
Author: { fields: "shortSlugOption" },
testing: { fields: "mediaaccess" },
dummy: { fields: "fieldtest" },
new: { fields: "fieldtest" },
};
const data = [
{
system: {
id: "4gSSbjCFEorYXqrgDIP2FA",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
shortSlugOption: { "en-us": "some value", "za-op": "random value" },
mediaFileAssetLink: { "en-us": "some file", "za-op": "file Linl" },
mediaFileAssetGalary: { hi: "file link 2" },
singleMediaImage: { hi: "file link single", "en-us": "english link" },
},
},
{
system: {
id: "1aBOO8tu3lUsjtICuIbUM5",
type: "Entry",
content: { type: { name: "Author" } },
},
DataDetails: {
short: { "en-us": ["shorts", "values"], "za-op": "short details" },
shortSlugOption: { "hi-In": "options" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2pOUGnI1oRD7nsrYs600HA",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "details value" } },
},
{
system: {
id: "66rzYr2BpWL1VTBHdLTdSW",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: {
oneReference: { hi: "values 1" },
multiReference: { "hi-In": "values 2" },
},
},
{
system: {
id: "cIb5mqEBRWDD6hrNmFmFE",
type: "Entry",
content: { type: { name: "new" } },
},
DataDetails: { testingNewValue: { "hi-IN": "jksdsdo" } },
},
{
system: {
id: "7kRzyt4PFrX13gHcw3Z1Ko",
type: "Entry",
content: { type: { name: "testing" } },
},
DataDetails: { testingNewValue: { "en-us": "kknksdo" } },
},
{
system: {
id: "2OspeCtNK0sh2cPiuU9jIz",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
short: { "za-op": "dfvndkssa" },
shortSlugOption: { hi: "sdocjosmdc" },
mediaFileAssetLink: { "en-us": "jdsojocis" },
booleanField: { "hi-In": "true" },
},
},
{
system: {
id: "2eAmIIuG4xkLvatkU3RUSy",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
dummy: { "en-us": "dshcifdvk" },
india: { "za-op": "sdci", hi: "hewd" },
},
},
{
system: {
id: "7hbdS3MgfZ73TOtlu1WfXw",
type: "Entry",
content: { type: { name: "dummy" } },
},
DataDetails: {
testingNewValue: { "en-us": "sdcoklsdc" },
locationField: { hi: "sdcndkdc" },
},
},
];
Object.keys(field).map((type) => {
const searchKey = field[type]?.fields;
result = data.reduce(
(
r,
{
system: {
id,
content: {
type: { name },
},
},
DataDetails,
}
) => {
r[name] ??= {};
Object.entries(DataDetails).forEach(([key, object]) => {
Object.entries(object).forEach(([loc, value]) => {
r[name][loc] ??= {};
r[name][loc][id] ??= {subject:"hello", id:id, address:`${name}/hello`};
r[name][loc][id][key] = value;

});
});
console.log(r);
return r;
},
{}
);

const typeObject = result[type] ?? {};
let subject;
outer: for (key in typeObject) {
const value = typeObject[key];
subject = null;
for (subkey in value) {
const subvalue = value[subkey];
subject = subvalue[searchKey];
if (subject != null) break;
}
console.log(subject ?? "Unknown");
}
});

代替hello我试图获取field对象中存在的shortSlugOption值和其他值,但如果shortSlugOption存在于en-us za-op or hi中,那么如果shortSlugOption不存在,它应该从这个键中获取第一个值en-us那么它应该在za-op时以en-us打印unknown并分配该值,但如果在en-us and za-op该值不存在,那么它应该打印unknown并像这样检查其他hi,但正如我们所看到的Author en-us 1aBOO8tu3lUsjtICuIbUM5不包含shortSlugOption那么它应该打印unknown代替hello

在这种情况下,您好,我会说尝试使用数组函数会使代码太不清楚。 有时您可能需要使用函数;)

let result = {}
const mapMedia = (val) => {
if (val instanceof Array) {
return val.map(mapMedia).filter(i => !!i)
}
if (val && val.conf && val.conf.type === 'media' && val.conf.id) {
return mediaObj[val.conf.id] || null
}
return undefined
}
const getLocalizedDataForAuthor = (locale, author) => {
return result && result[locale] && result[locale][author] || null
}
const addLocalizedDataForAuthor = (locale, author, key, value) => {
if (key && value) {
if (!result[locale]) { // create the locale in result if not existing
result[locale] = {}
}
if (!result[locale][author]) { // create the author in result if not existing
result[locale][author] = {}
}
result[locale][author][key] = value
}

}
data.forEach(authorInitialData => { // authorInitialData : {system, DataDetails}
for (const key in authorInitialData.DataDetails) {
const localizedData = authorInitialData.DataDetails[key]
for (const locale in localizedData) {
const authorFinalData = {}
var finalData = localizedData[locale]
const media = mapMedia(finalData)
if (media !== undefined) { // is not a media
addLocalizedDataForAuthor(locale, authorInitialData.system.id, key, media)
} else {
addLocalizedDataForAuthor(locale, authorInitialData.system.id, key, finalData)
}
const author = getLocalizedDataForAuthor(locale, authorInitialData.system.id)
if (author) {
author.subject = author.subject || 'unknown'
author.address = author.address || '/new/unknown'
}
}
}
})
console.log(result)

如果你真的想只使用数组函数,你会使用Object.keys(yourObject).arrayFunction( arrayFunctionCallback )

最新更新