Я пытался использовать существующие примеры карт и сокращать методы в javascript с большим трудом. Ниже я включил исходные данные и целевые данные. Я хотел бы преобразовать исходные данные в целевые данные. Может ли кто-нибудь помочь показать, как этого можно достичь, просто используя javascript?
Исходный формат
const source = [
{
id: 141,
event: {
id: 11,
name: "Event 1",
created: "2020-03-31T18:02:19+00:00",
updated: "2020-04-01T14:05:13+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
event_type: {
id: 1,
name: "VIP"
},
url: "https://eventone.com",
vsms_url: "https://eventone.com",
prefix: "https://eventone.com"
},
model: {
id: 15,
franchise: {
id: 5,
name: "Honda",
logo: null,
created: "2020-03-31T14:21:11+00:00",
updated: "2020-03-31T14:21:11+00:00",
status: {
id: 2,
name: "Work in progress"
},
franchise_models: [
null,
{
id: 8,
franchise: null,
name: "Jazz",
image: null
}
],
branch: [
{
id: 19,
name: "Branch Four",
logo: null,
created: "2020-03-31T17:06:24+00:00",
updated: "2020-04-17T14:05:38+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
franchise: null,
address_1: null,
address_2: null,
address_3: null,
postcode: null,
telephone: null,
from_email: null,
to_email: null,
contact_email: "testemail@test.com"
}
]
},
name: "Civic",
image: null
}
},
{
id: 142,
event: {
id: 11,
name: "Event 1",
created: "2020-03-31T18:02:19+00:00",
updated: "2020-04-01T14:05:13+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
event_type: {
id: 1,
name: "VIP"
},
url: "https://eventone.com",
vsms_url: "https://eventone.com",
prefix: "https://eventone.com"
},
model: {
id: 8,
franchise: {
id: 5,
name: "Honda",
logo: null,
created: "2020-03-31T14:21:11+00:00",
updated: "2020-03-31T14:21:11+00:00",
status: {
id: 2,
name: "Work in progress"
},
franchise_models: [
{
id: 15,
franchise: null,
name: "Civic",
image: null
},
null
],
branch: [
{
id: 19,
name: "Branch Four",
logo: null,
created: "2020-03-31T17:06:24+00:00",
updated: "2020-04-17T14:05:38+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
franchise: null,
address_1: null,
address_2: null,
address_3: null,
postcode: null,
telephone: null,
from_email: null,
to_email: null,
contact_email: "testemail@test.com"
}
]
},
name: "Jazz",
image: null
}
},
{
id: 143,
event: {
id: 11,
name: "Event 1",
created: "2020-03-31T18:02:19+00:00",
updated: "2020-04-01T14:05:13+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
event_type: {
id: 1,
name: "VIP"
},
url: "https://eventone.com",
vsms_url: "https://eventone.com",
prefix: "https://eventone.com"
},
model: {
id: 9,
franchise: {
id: 6,
name: "Volkswagen",
logo: null,
created: "2020-03-31T14:21:11+00:00",
updated: "2020-03-31T14:21:11+00:00",
status: {
id: 2,
name: "Work in progress"
},
franchise_models: [
{
id: 12,
franchise: null,
name: "Polo",
image: null
},
null
],
branch: [
{
id: 19,
name: "Branch Four",
logo: null,
created: "2020-03-31T17:06:24+00:00",
updated: "2020-04-17T14:05:38+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
franchise: null,
address_1: null,
address_2: null,
address_3: null,
postcode: null,
telephone: null,
from_email: null,
to_email: null,
contact_email: "testemail@test.com"
}
]
},
name: "fdsfdsfdsfdsf",
image: null
}
},
{
id: 144,
event: {
id: 11,
name: "Event 1",
created: "2020-03-31T18:02:19+00:00",
updated: "2020-04-01T14:05:13+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
event_type: {
id: 1,
name: "VIP"
},
url: "https://eventone.com",
vsms_url: "https://eventone.com",
prefix: "https://eventone.com"
},
model: {
id: 4,
franchise: {
id: 6,
name: "Volkswagen",
logo: null,
created: "2020-03-31T14:21:11+00:00",
updated: "2020-03-31T14:21:11+00:00",
status: {
id: 2,
name: "Work in progress"
},
franchise_models: [
{
id: 13,
franchise: null,
name: "Golf",
image: null
},
null
],
branch: [
{
id: 19,
name: "Branch Four",
logo: null,
created: "2020-03-31T17:06:24+00:00",
updated: "2020-04-17T14:05:38+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
franchise: null,
address_1: null,
address_2: null,
address_3: null,
postcode: null,
telephone: null,
from_email: null,
to_email: null,
contact_email: "testemail@test.com"
}
]
},
name: "fdsfdsfdsfdsf",
image: null
}
}
];
(*** Обратите внимание) В целевых данных :
Дублирующиеся франшизы объединяются по id. Модели франшизы объединены во множество моделей франшизы. ** Также обратите внимание, что нулевые значения в исходном franchise_models больше не находятся в целевом franchise_models.
Целевой формат
const target = [{
franchise_id: 5,
franchise_name: "Honda",
franchise_models: [{
id: 15,
franchise: null,
name: "Civic",
image: null
},
{
id: 8,
franchise: null,
name: "Jazz",
image: null
}],
branch: [
{
id: 19,
name: "Branch Four",
logo: null,
created: "2020-03-31T17:06:24+00:00",
updated: "2020-04-17T14:05:38+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
franchise: null,
address_1: null,
address_2: null,
address_3: null,
postcode: null,
telephone: null,
from_email: null,
to_email: null,
contact_email: "testemail@test.com"
}
]
}, {
franchise_id: 6,
franchise_name: "Vauxhall",
franchise_models: [{
id: 12,
franchise: null,
name: "Polo",
image: null
},
{
id: 13,
franchise: null,
name: "Golf",
image: null
}],
branch: [
{
id: 19,
name: "Branch Four",
logo: null,
created: "2020-03-31T17:06:24+00:00",
updated: "2020-04-17T14:05:38+00:00",
status: {
id: 2,
name: "Work in progress"
},
dealer: {
id: 1,
name: "Dealer 1",
logo: null,
aws_endpoint: null,
created: "2020-03-26T11:32:00+00:00",
updated: "2020-04-01T14:42:50+00:00",
status: {
id: 2,
name: "Work in progress"
}
},
franchise: null,
address_1: null,
address_2: null,
address_3: null,
postcode: null,
telephone: null,
from_email: null,
to_email: null,
contact_email: "testemail@test.com"
}
]
}]