Axios params возвращает данные внутри объекта вместо массива - PullRequest
0 голосов
/ 01 июня 2019

При использовании axios.get без параметров все данные возвращаются внутри массива, как и должно быть, но когда я использую параметры, все объекты возвращаются внутри объекта, и я не могу отобразить его.Как можно поместить все объекты внутри массива?Если я пытаюсь поместить его в массив, он помещает только тот объект, который содержит все нужные мне объекты.

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

возвращает примерно так

{…} count: 10 items:Объект {0: {…}, 2: {…}, 3: {…},…}: объект {…}

должен быть таким

Array (41) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…},…]

Ответы [ 2 ]

0 голосов
/ 01 июня 2019

попробуйте

axios.get('http://api.digiart.lt/items/',
        { params: 
            { category:"latte" }} )
    .then(response => {
        let coffee = Object.keys(response.items).map(i => response.items[i])
0 голосов
/ 01 июня 2019

Это ответ по умолчанию от 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`s a 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...