Estoy tratando de mapear una matriz en (React js) para averiguar si contiene un valor de "fotógrafo", por lo que puedo mostrar una imagen que dice que esta parte específica de la matriz está protegida por derechos de autor (partes de esta matriz no contiene "fotógrafo")
Esta es la matriz con la que estoy trabajando:
"collection": {
"version": "1.0",
"href": "http://images-api.nasa.gov/search?q=sun&page=1&media_type=image&year_start=2022&year_end=2022",
"items": [
{
"href": "https://images-assets.nasa.gov/image/ACD22-0003-004/collection.json",
"data": [
{
"album": [
"CAPSTONE"
],
"center": "ARC",
"title": "CAPSTONE facing the Sun (Illustration)",
"photographer": "Daniel J. Rutter",
"nasa_id": "ACD22-0003-004",
"media_type": "image",
"keywords": [
"CAPSTONE",
"NRHO",
"Cubesat",
"Artemis"
],
"date_created": "2022-02-02T00:00:00Z",
"description": "CAPSTONE, a microwave oven-sized CubeSat, will fly in cislunar space – the orbital space near and around the Moon. The mission will demonstrate an innovative spacecraft-to-spacecraft navigation solution at the Moon from a near rectilinear halo orbit slated for Artemis' Gateway. Illustration by Daniel Rutter."
}
],
"links": [
{
"href": "https://images-assets.nasa.gov/image/ACD22-0003-004/ACD22-0003-004~thumb.jpg",
"rel": "preview",
"render": "image"
}
]
}
así es como he mapeado esta matriz:
{data?.collection?.items?.map((items, index) => {
if(
**items.data.photographer is true (here is where I'm stuck!**
)
{
return (
*Something specific*
)
} else {
return(
something else.)}
tenga en cuenta que estoy usando
¿datos?.colección?.elementos?.mapa((elementos, índice) =>...
porque quiero mostrar primero {items.links}.
También intenté crear una const...
const map1 = data.collections.items.data.map(data => data);
después:
if ( map1.photographer) return ( something)
sin embargo, eso no funcionó. Tengo la sensación de que lo escribí mal.
¡Gracias a todos por su ayuda! ¡Saludos!
Usar Array.prototype.some() para identificar la propiedad "fotógrafo" en cualquiera de los elementos del campo de datos.
Prueba así:
function App() {
const data = {
collection: {
version: "1.0",
href:
"http://images-api.nasa.gov/search?q=sun&page=1&media_type=image&year_start=2022&year_end=2022",
items: [
{
href:
"https://images-assets.nasa.gov/image/ACD22-0003-004/collection.json",
data: [
{
album: ["CAPSTONE"],
center: "ARC",
title: "CAPSTONE facing the Sun (Illustration)",
photographer: "Daniel J. Rutter",
nasa_id: "ACD22-0003-004",
media_type: "image",
keywords: ["CAPSTONE", "NRHO", "Cubesat", "Artemis"],
date_created: "2022-02-02T00:00:00Z",
description:
"CAPSTONE, a microwave oven-sized CubeSat, will fly in cislunar space – the orbital space near and around the Moon. The mission will demonstrate an innovative spacecraft-to-spacecraft navigation solution at the Moon from a near rectilinear halo orbit slated for Artemis' Gateway. Illustration by Daniel Rutter."
}
],
links: [
{
href:
"https://images-assets.nasa.gov/image/ACD22-0003-004/ACD22-0003-004~thumb.jpg",
rel: "preview",
render: "image"
}
]
}
]
}
};
return (
<div>
{data.collection.items.map((item, index) => {
const copyRighted = item.data.some((d) =>
d.hasOwnProperty("photographer")
);
return (
<React.Fragment key={index}>
<div>{copyRighted && "Copy Righted"}</div>
<div>{item.href}</div>
</React.Fragment>
);
})}
</div>
);
}
ReactDOM.render(<App />, document.querySelector('.react'));
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<div class='react'></div>