双重映射在React+Typescript中

huangapple go评论46阅读模式
英文:

Double map in React+Typescript

问题

下面是你要的代码部分的中文翻译:

也许我漏掉了什么但在这种情况下我应该如何正确地进行双重映射因为我在第二个映射上遇到了错误属性map在类型{ 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>
);

需要映射的部分是在result中的bounds下的departure属性中的name。如果你需要获取这个属性,你可以在map函数中使用value.bounds.departure.name

英文:

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&lt;ConversionData[]&gt;([]);
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(() =&gt; {
const api = async () =&gt; {
const data = await fetch(&quot;http://localhost:3001/flights&quot;).then((res) =&gt;
res.json()
);
setResult(data);
console.log(result);
};
api();
}, []);
return (
&lt;div className=&quot;App&quot;&gt;
&lt;h1&gt;
{result?.map((value) =&gt; {
console.log(value)
return (
&lt;div&gt;
&lt;div&gt;{value.price.amount}&lt;/div&gt;
{value.bounds.map((newvalue:any)=&gt;{
&lt;div&gt;{newvalue.departure.name}&lt;/div&gt;
})}
&lt;/div&gt;
);
})}
&lt;/h1&gt;
&lt;/div&gt;
);

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

答案1

得分: 0

你的类型ConversionData中的边界是对象。但在你的数据截图中,边界应该是数组。

bounds: Array&lt;{
  departure: {
    code: string;
    name: string;
    dateTime: string;
  };
  destination: {
    code: string;
    name: string;
    dateTime: string;
  };
  duration: string;
  };
}&gt;;
英文:

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


bounds: Array&lt;{
departure: {
code: string;
name: string;
dateTime: string;
};
destination: {
code: string;
name: string;
dateTime: string;
};
duration: string;
};
}&gt;;

huangapple
  • 本文由 发表于 2023年2月19日 23:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定