简体   繁体   English

我有一个包含图像和文档的对象数组,我想检查 mime_type 并选择要显示的第一个元素(React)<img> 标签

[英]I have a array of objects containing images and documents, I want to check the mime_type and pick the first element (React) to display in <img> tag

I have a array of objects which contain images and documents, I want to check is the mime_type is 'images/jpeg' or 'image/png' and then only display the first image in a tag.我有一个包含图像和文档的对象数组,我想检查 mime_type 是“images/jpeg”还是“image/png”,然后只显示标签中的第一个图像。 I am using react.我正在使用反应。

I tried this but I keep getting undefined我试过了,但我一直不确定

<img className={"img-fluid img-list"} 
   src={this.state.images.filter(img => (img.mime_type === 'image/jpeg' || img.mime_type === 'image/png') || {})[0].blob_url} 
   alt="" />


<img className={"img-fluid img-list"} 
   src={this.state.images.filter(img => (img.mime_type === 'image/jpeg' || img.mime_type === 'image/png'))[0].blob_url} 
   alt="" />

if check with a {} i dont get any links如果用 {} 检查我没有得到任何链接

any help much appriciated.任何帮助都非常感谢。

You have an error here:你在这里有一个错误:

this.state.images.filter(img => (img.mime_type === 'image/jpeg' || img.mime_type === 'image/png') || {})[0].blob_url

It should be:它应该是:

((this.state.images.filter(img => (img.mime_type === 'image/jpeg' || img.mime_type === 'image/png')) || [])[0] || {}).blob_url

If you add a bit of indentation, it should be more clear where your error was:如果您添加一些缩进,应该更清楚您的错误在哪里:

(
  (
    this.state.images.filter(img => (
      img.mime_type === 'image/jpeg' || img.mime_type === 'image/png'
    ))     // give me an array with only the images
    || []  // OR give me back an empty array if you cannot find any
  )[0]     // then give me the first element (image object) of the array
  || {}    // OR give me back an empty object if the array is empty
).blob_url // finally give me the `blobl_url` of the object

Alternatively, you can use Array.prototype.find , which will return the first item matching the criteria:或者,您可以使用Array.prototype.find ,它将返回符合条件的第一个项目:

(this.state.images.find(img => (img.mime_type === 'image/jpeg' || img.mime_type === 'image/png')) || {}).blob_url

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法显示图像<img>当我在对象数组中有图像时在 Reactjs 中标记 - Not able to display images in <img> tag in Reactjs when i have images in Array of objects JavaScript:我有一个数组。 我想检查第一个元素是否为字段集 - JavaScript: I have an array. I want to check if the first element is a fieldset 我想创建一个js文件,并且必须动态加载img标签 - I want to create a js file and have to load img tag dynamically 如何检查A标签是否环绕IMG元素? - How do I check if an A tag is wrapping around an IMG element? 我有两个对象数组,我想更新具有相同 messageId 的对象,并在反应 javascript 中保留唯一的一个数组 - i have two array of objects, i want to update the one which have same messageId and keep the unique one in one array in react javascript 从对象数组中,我想从数组中随机选择一个 object 并设置该对象的一个变量 - From an array of objects, I want to pick a random object from the array and set one of that object's variables 我想在图像的路径内写图标名称 <img source=“”> 标签 - I want to write icon names inside the paths of images <img source=“”> tag 我想使用 react 以 3*X 的网格方式显示图像 - I want to display images in a grid fashion of 3*X using react 我有数组列表,我想渲染它。 React + redux - I have list of array and i want to render it . React+redux 包含一个元素的数组包含许多我想成为数组元素的对象 - Array containing one element containing a number of objects which I'd like to be elements of array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM