操作get(this.props'data')的数据输出(Manipulate data output from get(this.props 'data'))

编程入门 行业动态 更新时间:2024-10-11 01:10:17
操作get(this.props'data')的数据输出(Manipulate data output from get(this.props 'data'))

我使用ReactJSGatsbyJS以及使用GraphQLContentful提取的数据

获取数据是可以的,但是在这种情况下,使用react-photo-gallery ( Github )所提取的数据与要求不符。

这是一个GraphQL查询的例子

query galleryQuery($slug: String!) { contentfulImageGallery(slug: {eq: $slug}) { summerGallery { sizes(maxWidth: 1500) { ...GatsbyContentfulSizes_withWebp_noBase64 } } } }

并通过定义数据

const summerGallery = get(this.props, 'data.contentfulImageGallery')

我得到了一个JSON响应

{ "data": { "contentfulImageGallery": { "summerGallery": [ { "sizes": { "aspectRatio": 1.7779433681073025, "sizes": "(max-width: 1500px) 100vw, 1500px", "src": "http://example.com/example/img1.jpg?w=1500&q=50" } }, { "sizes": { "aspectRatio": 1.7779433681073025, "sizes": "(max-width: 1500px) 100vw, 1500px", "src": "http://example.com/example/img2.jpg?w=1500&q=50" } } ] } } }

但我怎么操纵这些数据,以便我可以使用其中的图像

<Gallery photos={summerGallery}/>

const summerGallery = [ { src: 'http://example.com/example/img1.jpg' width: "NEED WIDTH" height: "NEED HEIGHT" }, { src: 'http://example.com/example/img2.jpg' width: "NEED WIDTH" height: "NEED HEIGHT" } ]

虽然我可以通过{summerGallery.sizes.src}得到src,但是它在控制台中返回undefined并且TypeError: Cannot read property 'src' of undefined页面TypeError: Cannot read property 'src' of undefined 。 宽度和高度,零的想法。

I am using ReactJS and GatsbyJS with data pulled from Contentful using GraphQL.

Getting the data is okay but on this occasion, using the react-photo-gallery (Github) the data pulled does not match requirements.

Here is an example of the GraphQL Query

query galleryQuery($slug: String!) { contentfulImageGallery(slug: {eq: $slug}) { summerGallery { sizes(maxWidth: 1500) { ...GatsbyContentfulSizes_withWebp_noBase64 } } } }

And by defining the data

const summerGallery = get(this.props, 'data.contentfulImageGallery')

I get a JSON response of

{ "data": { "contentfulImageGallery": { "summerGallery": [ { "sizes": { "aspectRatio": 1.7779433681073025, "sizes": "(max-width: 1500px) 100vw, 1500px", "src": "http://example.com/example/img1.jpg?w=1500&q=50" } }, { "sizes": { "aspectRatio": 1.7779433681073025, "sizes": "(max-width: 1500px) 100vw, 1500px", "src": "http://example.com/example/img2.jpg?w=1500&q=50" } } ] } } }

But how can I manipulate this data so that I can use the images within

<Gallery photos={summerGallery}/>

as

const summerGallery = [ { src: 'http://example.com/example/img1.jpg' width: "NEED WIDTH" height: "NEED HEIGHT" }, { src: 'http://example.com/example/img2.jpg' width: "NEED WIDTH" height: "NEED HEIGHT" } ]

I though that I could get the src by {summerGallery.sizes.src} but it returns undefined in the console and a TypeError: Cannot read property 'src' of undefined on page. The width and height, zero idea.

最满意答案

summerGallery.sizes.src未定义,因为summerGallery是一个没有sizes属性的数组。

您可以将summerGallery数组map为所需的格式:

const mappedDataArray = summerGallery.map(g => { return { src: g.sizes.src, width: '100vw', height: '1500px', } })

summerGallery.sizes.src is undefined because summerGallery is an Array with no sizes property.

You can map the summerGallery array to the required format:

const mappedDataArray = summerGallery.map(g => { return { src: g.sizes.src, width: '100vw', height: '1500px', } })

更多推荐

本文发布于:2023-04-29 09:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   数据   props   data   Manipulate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!