【发布时间】:2017-10-03 03:21:14
【问题描述】:
这是我的数据结构(缩写,你可以跳过这个,直到你在下面找到我问题的核心):
var data = {
jsonapi: { version: '1.0' },
data: {
type: 'mobile_screens',
id: '1',
attributes: { title: 'Watch' },
relationships: {
mobile_screen_components: {
data: [
{ type: 'mobile_screen_components', id: '1_1' },
{ type: 'mobile_screen_components', id: '1_2' },
],
},
},
},
included: [
{
type: 'videos',
id: '5590720024001',
attributes: {
thumbnail: {
width: 1280,
url:
'http://media2.XXXXX.com/BrightCove/694940094001/2017/09/27/694940094001_5590729403001_5590720024001-vs.jpg?pubId=694940094001',
height: 720,
},
start_air_date: '2017-09-27T16:32:00.000Z',
streams: [
{
url:
'http://XXXXX-f.akamaihd.net/i/BrightCove/694940094001/2017/09/27/694940094001_55907,27023001_5590720024001,30873001_5590720024001,32416001_5590720024001,32423001_5590720024001,32792001_5590720024001,32896001_5590720024001,.mp4.csmil/master.m3u8',
mime_type: 'MP4',
},
],
last_modified_date: '2017-09-27T16:50:13.471Z',
description: 'House Republicans renew call amid fresh Comey concerns',
},
},
{
type: 'videos',
id: '5590670324001',
attributes: {
...
},
},
{
type: 'videos',
id: '5590665282001',
attributes: {
...
},
},
{
id: '20fb845a-72ad-4d5f-a27a-c01efd857f76',
type: 'articles',
attributes: {
fn__legacy_type: 'articles',
title: 'Restaurant plans ginormous site near Wall Street, Ground Zero',
fn__media_tags: ['business'],
last_published_date: '2017-09-20T18:20:29.526Z',
thumbnail: {
image: {
url:
'https://secure.media.foxnews.com/BrightCove/990505083001/990505083001/2017/09/13/990505083001_5575086468001_5574853539001-vs.jpg?pubId=694940094001',
},
},
},
relationships: {
components_relationships: {
data: [
{
id: 'article_id_7',
type: 'videos',
},
{
id: 'article_id_8',
type: 'videos',
},
],
},
},
},
{
id: 'e6e327ec-ebb2-4220-a391-41f798e654f7',
type: 'articles',
attributes: {
...
},
relationships: {
...
},
},
{
type: 'mobile_screen_components',
id: '1_1',
attributes: { display_type: 'shelf', title: 'Featured Playlist' },
relationships: {
videos: {
data: [
[
{ type: 'videos', id: '5590720024001' },
{ type: 'videos', id: '5590670324001' },
{ type: 'videos', id: '5590665282001' },
{
id: '20fb845a-72ad-4d5f-a27a-c01efd857f76',
type: 'articles',
},
{
id: 'e6e327ec-ebb2-4220-a391-41f798e654f7',
type: 'articles',
},
],
},
articles: {
data: [
{
id: '20fb845a-72ad-4d5f-a27a-c01efd857f76',
type: 'articles',
},
{
id: 'e6e327ec-ebb2-4220-a391-41f798e654f7',
type: 'articles',
},
],
}
},
},
{
type: 'videos',
id: '1241186546001',
attributes: {
...
},
},
{
type: 'videos',
id: '1251429410001',
attributes: {
...
},
},
{
type: 'mobile_screen_components',
id: '1_2',
attributes: { display_type: 'shelf', title: 'Live TV' },
relationships: {
videos: {
data: [
{ type: 'videos', id: '1241186546001' },
{ type: 'videos', id: '1251429410001' },
],
},
},
},
{
type: 'videos',
id: '2013931500001',
attributes: {
...
},
},
{
type: 'mobile_screen_components',
id: '1_3',
attributes: { display_type: 'shelf', title: 'Live Streams' },
relationships: {
videos: {
data: [{ type: 'videos', id: '2013931500001' }],
},
},
},
],
};
对于included 数组中的每个对象,我想检查它是否具有“mobile_screen_components”的type。如果是这样,我想获取该父对象的 relationship 属性中的所有对象(例如
videos、articles、shows 等)并获取每个相应的 data 数组。最后,所有这些数组将被连接起来,并放在一个新的data 数组下,该数组将是items 数组的一个属性,现在是原始对象的唯一属性。
也就是说
{
type: 'mobile_screen_components',
id: '1_1',
attributes: { display_type: 'shelf', title: 'Featured Playlist' },
relationships: {
videos: {
data: [
[
{ type: 'videos', id: '5590720024001' },
{ type: 'videos', id: '5590670324001' },
{ type: 'videos', id: '5590665282001' }
]
},
articles: {
data: [
{
id: '20fb845a-72ad-4d5f-a27a-c01efd857f76',
type: 'articles',
},
{
id: 'e6e327ec-ebb2-4220-a391-41f798e654f7',
type: 'articles',
},
],
}
},
}
会变成
{
type: 'mobile_screen_components',
id: '1_1',
attributes: { display_type: 'shelf', title: 'Featured Playlist' },
relationships: {
items: {
data: [
[
{ type: 'videos', id: '5590720024001' },
{ type: 'videos', id: '5590670324001' },
{ type: 'videos', id: '5590665282001' },
{
id: '20fb845a-72ad-4d5f-a27a-c01efd857f76',
type: 'articles',
},
{
id: 'e6e327ec-ebb2-4220-a391-41f798e654f7',
type: 'articles',
},
],
],
},
},
}
我想使用一个镜头来返回整个结构,只应用这个转换。我最初的尝试不起作用:
const getMCRel = (type, rels) => {
if (type === 'mobile_screen_components' ) {
return rels
}
return undefined
}
const findMCRel = R.compose(getMCRel, R.props(['type','relationships']));
const rLens = R.lens(findMCRel, R.assoc('relationships'));
const itemsWrapper = (arr) => ({"items": arr});
const result = R.map(R.over(rLens,R.compose(itemsWrapper, R.values)),data.included)
【问题讨论】:
-
您可能想查看How to Create a Minimal, Complete, and Verifiable Example。样本中似乎有太多无关信息。
-
表示复杂性很棘手,但我删掉了一点
标签: arrays json functional-programming json-api ramda.js