Если запустить код, то видно что по sourceType "application.k8s" создаеться два объекта но они оба имеют одинаковый объект "parent"
{
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"
}
Хотя они должны иметь разный объект "parent" потому что в масиве "policyReferences" по sourceType "application.k8s" есть 2 разных объекта это видно по полю "dataType". Тоесть должны быть разные значение а не первый элемент массиваю Помогите пожалуйста решить проблему
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())