javascript - 如何对对象数组中的键进行排序并仅渲染一个对象
问题描述
我有一组行星对象,我正在尝试创建一个类似于其中一个行星的函数,并且只渲染一个行星。
我试图创建一个函数来从数组中挑选出一个行星,但我没有成功。
sortPlanet() {
const planet = this.state.sort((a, b) => a.index > b.index )
}
我试图在地图之前制作一个类型
const { data } = this.state
const planets = data
.sort((a, b) => a.index > b.index )
.map((planet, index) => {
return (<div key={index} className="app-container">
<div>
<h1>{planet.name}</h1>
</div>
<div className="about-container">
<span>Population: {planet.population}</span>
<span>Climate: {planet.climate}</span>
<span>Terrain: {planet.terrain}</span>
<br />
<span>{planet.films.length}</span>
</div>
</div>)
})
import './App.css';
class App extends Component {
state = {
data: [],
}
componentDidMount() {
const url = 'https://swapi.co/api/planets/'
fetch(url)
.then(response => response.json())
.then(response => {
this.setState({
data: response.results,
})
})
}
render() {
const { data } = this.state
const planets = data
.map((planet, index) => {
return (<div key={index} className="app-container">
<div>
<h1>{planet.name}</h1>
</div>
<div className="about-container">
<span>Population: {planet.population}</span>
<span>Climate: {planet.climate}</span>
<span>Terrain: {planet.terrain}</span>
<br />
<span>{planet.films.length}</span>
</div>
</div>)
})
console.log(planets)
return (
<div className="App">
{planets}
<button>Next</button>
</div>
)
}
}
export default App;
我的目标是仅对要显示的一个行星进行分类,并且每当我单击“下一步”按钮时,都会调用该函数来随机搜索另一个行星。
编辑:添加示例数据
[
{
"name": "Alderaan",
"rotation_period": "24",
"orbital_period": "364",
"diameter": "12500",
"climate": "temperate",
"gravity": "1 standard",
"terrain": "grasslands, mountains",
"surface_water": "40",
"population": "2000000000",
"residents": [
"https://swapi.co/api/people/5/",
"https://swapi.co/api/people/68/",
"https://swapi.co/api/people/81/"
],
"films": [
"https://swapi.co/api/films/6/",
"https://swapi.co/api/films/1/"
],
"created": "2014-12-10T11:35:48.479000Z",
"edited": "2014-12-20T20:58:18.420000Z",
"url": "https://swapi.co/api/planets/2/"
},
{
"name": "Yavin IV",
"rotation_period": "24",
"orbital_period": "4818",
"diameter": "10200",
"climate": "temperate, tropical",
"gravity": "1 standard",
"terrain": "jungle, rainforests",
"surface_water": "8",
"population": "1000",
"residents": [],
"films": [
"https://swapi.co/api/films/1/"
],
"created": "2014-12-10T11:37:19.144000Z",
"edited": "2014-12-20T20:58:18.421000Z",
"url": "https://swapi.co/api/planets/3/"
},
{
"name": "Hoth",
"rotation_period": "23",
"orbital_period": "549",
"diameter": "7200",
"climate": "frozen",
"gravity": "1.1 standard",
"terrain": "tundra, ice caves, mountain ranges",
"surface_water": "100",
"population": "unknown",
"residents": [],
"films": [
"https://swapi.co/api/films/2/"
],
"created": "2014-12-10T11:39:13.934000Z",
"edited": "2014-12-20T20:58:18.423000Z",
"url": "https://swapi.co/api/planets/4/"
}
]
解决方案
也许我误解了你想要做什么。似乎您不需要对它们进行排序。
如果你想要一个随机的星球使用
Math.floor(Math.random() * this.state.data.length)
您可以在您的状态下使用 2 个数组,而不仅仅是一个。一个数组,您从中获取一个随机行星,一个您正在设置顺序。
如果你想渲染一个行星,你不应该映射所有的行星。我会将您的状态更改为
state = {
data: [],
chosenPlanet: 0
}
并使用
const index = this.state.chosenPlanet;
const planet= this.state.data[index]
const renderPlanet =
(<div className="app-container">
<div>
<h1>{planet.name}</h1>
</div>
<div className="about-container">
<span>Population: {planet.population}</span>
<span>Climate: {planet.climate}</span>
<span>Terrain: {planet.terrain}</span>
<br />
<span>{planet.films.length}</span>
</div>
</div>)
推荐阅读
- deployment - 单个节点上的 Service Fabric 无状态服务部署
- point-cloud-library - pcl::NormalEstimation setSearch方法说明
- c# - 如何从 Slack Windows 应用程序中捕获聊天对话
- ngrx-effects - 如何正确使用 jasmine-marbles 测试 ofType 中的多个操作
- apache-spark - Pyspark 命令无法识别(Ubuntu)
- python - openpyxl 根据条件删除行
- c# - 项目的配置外部文件夹中的连接字符串
- javascript - 使用空检查创建速记对象的 ES6 等效代码
- c++ - 矩阵类中的任意矩阵维数
- python - 如何将pyspark模型保存到pickle文件中