繁体   English   中英

Axios params返回对象内的数据而不是数组

[英]Axios params returns data inside an object instead of array

当使用没有params的axios.get时,所有数据都会在数组内部返回,就像它应该的那样,但是当我使用params时,所有对象都返回到一个对象中并且我可以映射它。 如何将所有对象放在数组中? 如果我尝试将它放在数组中,它只会放置包含我需要的所有对象的对象。

     axios.get('http://api.digiart.lt/items/',
        { params: 
            { category:"latte" }} )
    .then(response => {
        let coffee = response.data;
        this.props.onLoadData(coffee);
        console.log(response.data);

像这样回来

{...}计数:10项:对象{0:{...},2:{...},3:{...},...}:对象{...}

应该是这样的

数组(41)[{...},{...},{...},{...},{...},{...},{...},{...},{...},{...},...]

这是http://api.digiart.lt/items/ API的默认响应

使用Object.keys将其映射到数组。

 // Request -> http://api.digiart.lt/items?category=latte var response = { "items": { "0": { "title": "Abbey's White Chocolate Latte", "description": "I created this recipe for my little sister, Abbey, who`sa latte fanatic. She was blown away, and drained it to the last drop! This makes one VERY large, indulgent latte, and could probably serve two. Enjoy!\\n\\nIngredients:\\n1 1\\/2 cups milk\\n1 tablespoon heavy cream\\n1\\/8 teaspoon vanilla extract\\n1 tablespoon white sugar\\n1\\/2 cup brewed espresso\\n1\\/4 cup white chocolate chips, chopped", "url": "https:\\/\\/www.allrecipes.com\\/recipe\\/137332\\/abbeys-white-chocolate-latte\\/", "category": "latte", "date": "2018-05-31", "img": "https:\\/\\/images.media-allrecipes.com\\/userphotos\\/560x315\\/2107268.jpg" }, "2": { "title": "Brown Sugar-Caramel Latte", "description": "Sometimes coffee is a dessert in itself. This is one of my favorite morning treats to make a Monday seem less intimidating. You'll need a battery-powered milk frother or it's just not the same.\\n\\nIngredients:\\n1 tablespoon brown sugar\\n1\\/4 cup half-and-half\\n1 tablespoon caramel ice cream topping\\n3\\/4 cup hot, brewed coffee", "url": "https:\\/\\/www.allrecipes.com\\/recipe\\/139119\\/brown-sugar-caramel-latte\\/", "category": "latte", "date": "2017-08-22", "img": "https:\\/\\/images.media-allrecipes.com\\/userphotos\\/560x315\\/707064.jpg" } // .. More items } } var arr = [] Object.keys(response.items).forEach(key => arr.push(response.items[key])) console.log(arr) 

尝试这个

axios.get('http://api.digiart.lt/items/',
        { params: 
            { category:"latte" }} )
    .then(response => {
        let coffee = Object.keys(response.items).map(i => response.items[i])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM