Ad
How To Access The Img Value? Javascript
I have an array of objects with several properties, one of them is called 'file', of which there are two types as represented below
const products = [
{
functional_id: "2_recharges",
quantity: 1,
title: "Coffret empreinte rouge",
file: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAA…em3dIEUUE1O4fn/M8l6v+f6VPgptdk2plAAAAAElFTkSuQmCC"
},
{
functional_id: "sacs_blancs",
quantity: 1,
title: "Sacs blancs",
file: "data:image/;base64,"
}
]
It's the first time I work with image files and I don't know how to differentiate to create a condition so that only those containing the word 'png' in its value are displayed, in the case of the example the first one, since those that are like the second one don't display any image
I tried to treat them as a string to check if they contained the value string but it doesn't work, as I understand that they are not strings.
If someone can give me an idea of how to create my condition. Thank you very much in advance
Ad
Answer
You will want to use the js filter
method to check whether the file includes 'png'
.
const products = [{
functional_id: "2_recharges",
quantity: 1,
title: "Coffret empreinte rouge",
file: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAA…em3dIEUUE1O4fn/M8l6v+f6VPgptdk2plAAAAAElFTkSuQmCC"
},
{
functional_id: "sacs_blancs",
quantity: 1,
title: "Sacs blancs",
file: "data:image/;base64,"
}
]
const pngImages = products.filter(product => product.file.includes('png'))
console.log(pngImages)
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad