gpt4 book ai didi

javascript - 如何对对象数组中的键进行排序并仅渲染一个对象

转载 作者:行者123 更新时间:2023-11-28 17:06:54 25 4
gpt4 key购买 nike

我有一系列行星对象,我正在尝试创建一个函数来处理其中一个行星并仅渲染一个行星。

我尝试创建一个函数来从数组中挑选出一颗行星,但没有成功。

sortPlanet() {
const planet = this.state.sort((a, b) => a.index > b.index )
}

我尝试在 map 之前创建一个类型

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>)

关于javascript - 如何对对象数组中的键进行排序并仅渲染一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55654073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com