Reactjs 在状态数组中响应更改数组
我在克隆阵列方面遇到问题 我将电影列表添加到API中,但它没有“键” 所以我想更改“API的ID”->“我的状态键” 及 “this.setState()”怎么办Reactjs 在状态数组中响应更改数组,reactjs,react-native,Reactjs,React Native,我在克隆阵列方面遇到问题 我将电影列表添加到API中,但它没有“键” 所以我想更改“API的ID”->“我的状态键” 及 “this.setState()”怎么办 抱歉,没有经验的解释您可以使用reduce,如下所示: const movieList = [ {id:1}, {id:2}, {id:3}] const keyMovieList = movieList.reduce((acc,rec)=>[...acc, {key:rec.id}], []) 请参见游乐场中的:为什么要使
抱歉,没有经验的解释您可以使用reduce,如下所示:
const movieList = [ {id:1}, {id:2}, {id:3}]
const keyMovieList = movieList.reduce((acc,rec)=>[...acc, {key:rec.id}], [])
请参见游乐场中的:为什么要使用key属性,而响应却没有这样做?是否希望新结构是key:number的数组?而不是id:number?可能性很低,但如果您想这样做以获得映射JSX的“键”,则不需要这样做。使用
id
作为键,因为它们是唯一的。如果这不是你的意图,那么请在你的问题中提供更多细节。我认为这是我见过的reduce最奇怪的用法。。。如果要更改数组中每个元素的结构,为什么不使用.mapconstkeymovielist=movieList.map(输入=>{return{key:input.id}})
const movieList = axios.get("URL.json"); //It is not having key value
const movieList = [ {id:1}, {id:2}, {id:3}]
const keyMovieList = movieList.reduce((acc,rec)=>[...acc, {key:rec.id}], [])