Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Double map in React+Typescript

Maybe I’m missing something, but how should I properly double map in this case? Bcs I have error on secon map : Property ‘map’ does not exist on type ‘{ departure: { code: string; name: string; dateTime: string; }; destination: { code: string; name: string; dateTime: string; }; duration: string; }

const [result, setResult] = useState<ConversionData[]>([]);

  type ConversionData = {
  uuid: string;
  airlinesCode: string;
  price: {
  amount: number;
  currency: string;
  };
  bounds: {
  departure: {
    code: string;
    name: string;
    dateTime: string;
  };
  destination: {
    code: string;
    name: string;
    dateTime: string;
  };
  duration: string;
   };
 };
 useEffect(() => {
   const api = async () => {
     const data = await fetch("http://localhost:3001/flights").then((res) =>
      res.json()
    );
    setResult(data);
  console.log(result);
};
 api();
}, []);

return (
 <div className="App">
   <h1>
     {result?.map((value) => {
       console.log(value)
       return (
         <div>
           <div>{value.price.amount}</div>
           {value.bounds.map((newvalue:any)=>{
             <div>{newvalue.departure.name}</div>
           })}
         </div>
       );
     })}
   </h1>
 </div>
 );

what I need to map

I’ve searched the internet for something similar, but I’ve hit the starting point, and I need to get to bounds -> departure -> name

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Bounds in your type ConversionData is object. But at you data screenshot bounds should be array.


bounds: Array<{
  departure: {
    code: string;
    name: string;
    dateTime: string;
  };
  destination: {
    code: string;
    name: string;
    dateTime: string;
  };
  duration: string;
   };
 }>;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading