Я пытаюсь реструктурировать мои извлеченные JSON данные. Пример моего JSON ответа приведен здесь:
{
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
Я хочу взять ключ fields и свойства из , включая> Актив и поместить их в правильные item> fields> picture с соответствующим ASSETID . Тогда включает будет просто удалено из всего этого, потому что оно больше не нужно.
Вот какой конечный результат мне нужен:
{
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
]
}
Что В настоящее время у меня есть функция, которая перебирает каждый элемент в items и пытается выяснить, содержит ли массив Assets ASSETID из элемента.
const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);
items.forEach(item => {
if (item.fields.picture !== undefined) {
const {
id
} = item.fields.picture.sys;
const mappedAssets = assets.map(asset => asset.sys.id);
if (mappedAssets.includes(id)) {
console.log('This id matched:', id)
}
}
})
const data = {
"sys": {
"type": "Array"
},
"total": 3,
"skip": 0,
"limit": 100,
"items": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID1",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "John",
"lastName": "Doe",
"title": "John's Title",
"building": "John's Building",
"roomNumber": 1234,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID1"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID2",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Jane",
"lastName": "Doe",
"title": "Jane's title",
"building": "Jane's Building",
"roomNumber": 4321,
"picture": {
"sys": {
"type": "Link",
"linkType": "Asset",
"id": "ASSETID2"
}
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ENTRYID3",
"type": "Entry",
"createdAt": "2019-05-22T15:03:51.318Z",
"updatedAt": "2019-05-22T15:03:51.318Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"contentType": {
"sys": {
"type": "Link",
"linkType": "ContentType",
"id": "contentTypeID"
}
},
"locale": "en-US"
},
"fields": {
"firstName": "Bob",
"lastName": "Doe",
"title": "Bob's title",
"building": "Bob's Building",
"roomNumber": 1111
}
}
],
"includes": {
"Asset": [{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID1",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "johndoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID1/1234567890/JohnDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JohnDoe.jpg",
"contentType": "image/jpeg"
}
}
},
{
"sys": {
"space": {
"sys": {
"type": "Link",
"linkType": "Space",
"id": "SPACEID"
}
},
"id": "ASSETID2",
"type": "Asset",
"createdAt": "2019-05-22T15:03:41.369Z",
"updatedAt": "2019-05-22T15:03:41.370Z",
"environment": {
"sys": {
"id": "master",
"type": "Link",
"linkType": "Environment"
}
},
"revision": 1,
"locale": "en-US"
},
"fields": {
"title": "janedoe",
"file": {
"url": "//some.image.link/SPACEID/ASSETID2/0987654321/JaneDoe.jpg",
"details": {
"size": 16681,
"image": {
"width": 244,
"height": 352
}
},
"fileName": "JaneDoe.jpg",
"contentType": "image/jpeg"
}
}
}
]
}
};
const items = data.items.slice(0);
const assets = data.includes.Asset.slice(0);
items.forEach(item => {
if (item.fields.picture !== undefined) {
const {
id
} = item.fields.picture.sys;
const mappedAssets = assets.map(asset => asset.sys.id);
if (mappedAssets.includes(id)) {
console.log('This id matched:', id)
}
}
})
Я не уверен, как распределить свойства так, как я хочу. Я двигаюсь в правильном направлении или я размышляю?
Решение в итоге получилось:
data.items.forEach(item => {
if (item.fields.picture !== undefined) {
const { id } = item.fields.picture.sys;
const foundAssets = data.includes.Asset.find(asset => asset.sys.id === id);
item.fields.picture = { ...item.fields.picture, fields: {...foundAssets.fields} };
}
});
delete data.includes;