将[array]从Fetch解析为[array]

时间:2018-09-03 11:22:37

标签: arrays reactjs fetch

好的,这是我来自API的console.log(data)

enter image description here 我正在像这样获取这些数据:

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"},
    ];

1 个答案:

答案 0 :(得分:2)

您需要调用.map集合上的data.items

const products = data.map(obj => ({title: obj.title, description: obj.description}))
this.setState({isLoaded: true, products})