[英]Accessing all properties in array of objects for React
I'm building a search engine for Heroes of Might and Magic III and I want to check that the name of a unit entered matched the name of a unit in the data base I built.我正在为《魔法门之英雄无敌III》构建一个搜索引擎,我想检查输入的单位名称是否与我建立的数据库中的单位名称匹配。 I currently have a large JSON file with an array of objects that each have a name property.
我目前有一个大型 JSON 文件,其中包含一组对象,每个对象都有一个 name 属性。 Currently the code I'm working on looks like this:
目前我正在处理的代码如下所示:
return (
<div className="main-content">
<h2>{name}</h2>
{name === CastleUnits[0].Name && <UnitContainer data={CastleUnits} />}
{name === CastleUnits[1].Name && <UnitContainer data={CastleUnits} />}
</div>
);
Where 'name' is what is passed in search and CastleUnits[0].Name works for the first unit.其中 'name' 是在搜索中传递的内容,CastleUnits[0].Name 适用于第一个单元。 I've tried to solve this looping through with a for loop but I received an error.
我试图用一个 for 循环来解决这个循环,但我收到了一个错误。
You need to filter and map through the array like this:您需要像这样通过数组过滤和 map :
return (
<div className="main-content">
<h2>{name}</h2>
{CastleUnits.filter(e => e.Name = name).map(e => <UnitContainer data={e} />)}
</div>
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.