How to display the corresponding value from an array in an object property?
-
If you run the code, you can see that by sourceType "application.k8s" two objects are created, but they both have the same "parent" object.
{ dataType: "METRIC", name: "metricIngest_application_k8s_CUSTOM", parent: { dataType: "METRIC", name: "metricIngest_application_k8s", policyType: "INGEST", sourceType: "application.k8s" }, policyScope: "customDataSources", policyType: "INGEST", sourceType: "application.k8s" }, { dataType: "DATAMAP", name: "modelIngest_application_k8s_CUSTOM", parent: { dataType: "METRIC", name: "metricIngest_application_k8s", policyType: "INGEST", sourceType: "application.k8s" }, policyScope: "customDataSources", policyType: "INGEST", sourceType: "application.k8s" }
Although they must have a different "parent" object, because in the "policyReferences" array by sourceType "application.k8s" there are 2 different objects, this can be seen from the "dataType" field. That is, there should be different values and not the first element in the array. Please help me solve the problem.
const policyDataSources = { defaultDataSources: [ { policyReferences: [ { dataType: "EVENT", policyName: "eventIngest_cz", policyType: "INGEST" } ], sourceType: "CZ", }, { policyReferences: [ { dataType: "METRIC", policyName: "metricIngest_application_k8s", policyType: "INGEST" }, { dataType: "DATAMAP", policyName: "modelIngest_application_k8s", policyType: "INGEST" } ], sourceType: "application.k8s" }, { policyReferences: [ { dataType: "EVENT", policyName: "eventIngest_cz", policyType: "INGEST" } ], sourceType: "defaultTest" }, ], customDataSources: [ { policyReferences: [ { dataType: "METRIC", policyName: "testName", policyType: "INGEST" } ], sourceType: "testType" }, { policyReferences: [ { dataType: "EVENT", policyName: "eventIngest_cz_CUSTOM", policyType: "INGEST" } ], sourceType: "CZ", }, { policyReferences: [ { dataType: "METRIC", policyName: "metricIngest_application_k8s_CUSTOM", policyType: "INGEST" }, { dataType: "DATAMAP", policyName: "modelIngest_application_k8s_CUSTOM", policyType: "INGEST" } ], sourceType: "application.k8s" }, ], } function formattedDataSources() { if (policyDataSources) { let defaultDataSources = policyDataSources.defaultDataSources || [] let customDataSources = policyDataSources.customDataSources || [] const extractDefaultDataSourceType = defaultDataSources.map(el => el.sourceType) const extractCustomDataSourceType = customDataSources.map(el => el.sourceType) if (defaultDataSources.length > 0 && customDataSources.length > 0) { // extracting policy references from overwritten default datasource customDataSources = customDataSources.map((cds) => { const parent = defaultDataSources .find(dds => dds.sourceType === cds.sourceType) return { ...cds, ...(extractDefaultDataSourceType .includes(cds.sourceType)) && { parent: { dataType: parent.policyReferences[0].dataType, // думаю проблема здесь name: parent.policyReferences[0].policyName, policyType: parent.policyReferences[0].policyType, sourceType: parent.sourceType } }, } }) defaultDataSources = defaultDataSources.filter( dds => !extractCustomDataSourceType.includes(dds.sourceType) ) } // extracting all policy references which datasource has // and linking them to sourceType return Object.entries({ defaultDataSources, customDataSources }) .map(([k, v]) => v.map(({ policyReferences, ...ref }) => policyReferences .map(el => ({ name: el.policyName, ...el, ...ref, policyScope: `${k}`, })))) .flat(2) } return [] } console.log(formattedDataSources())
JavaScript Isabelle Hickman, Jun 30, 2019
0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!