好的,这是我来自API的console.log(data)
。
constructor(props) {
super(props);
this.state = {
data: [],
isLoaded: false,
displayCategory: "all",
products: PRODUCTS,
productCategories: PRODUCT_CATEGORIES
};
this.setCategory = this.setCategory.bind(this);
}
componentDidMount(){
fetch('http://localhost:50647/fund/GetFunds?MaxPageSize=100&Offset=5&Limit=5')
.then(response => {
return response.json();
}).then(data => {
this.setState({
isLoaded: true,
})
console.log(data);
}).catch(err => {
});
}
现在我需要将这些数据转换为这种类型的数组,以便以后进行排序和过滤。
当我在ComponentDidMount中有数据时,如何提取这些数据以访问其他组件?
const PRODUCTS = [
{title: "Titleofitem", description: "Descriptionofitem"},
{title: "Titleofitem", description: "Descriptionofitem"},
];
答案 0 :(得分:2)
您需要调用.map
集合上的data.items
。
const products = data.map(obj => ({title: obj.title, description: obj.description}))
this.setState({isLoaded: true, products})