无法在v-for循环上显示数组中的所有对象,仅显示最后一个对象

时间:2019-01-29 17:27:55

标签: arrays json vue.js v-for

我正在提取一些数据。我将这些数据按键分组,然后尝试在Vue的v-for循环中将其打印出来。

代码看起来像这样:

// I initialize an empty array in data
data() {
  return {
    locArray: []
  }
}


// This is done in the api call, in beforeRouteEnter()

const items = data.continental;

Array.prototype.groupBy = function(prop) {
  return this.reduce(function(groups, item) {
    const val = item[prop];
    groups[val] = groups[val] || [];
    groups[val].push(item);
    return groups;
  }, {});
};

for (let i in items) {
  let source = items[i];
  let details = source.details;
  let groupedByLocation = details.groupBy(location);
  vm.locArray = groupedByLocation
}

// This is an example of what the data looks like
{
  data: {
    continental: {
      countryOne: {
        weather: "weatherOne",
        details: [{
          location: "west",
          foods: "foodOne",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryTwo: {
        weather: "weatherTwo",
        details: [{
          location: "north",
          foods: "foodTwo",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryThree: {
        weather: "weatherThree",
        details: [{
          location: "north",
          foods: "foodThree",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryFour: {
        weather: "WeatherFour",
        details: [{
          location: "west",
          foods: "foodFour",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryfive: {
        weather: "WeatherFive",
        details: [{
          location: "north",
          foods: "foodFive",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      }
    }
  }
}
<div class="entry-content">
  <div class="single-entry" v-for="loc in locArray" :key="loc.id">
    <span class="test-wrap">{{ loc }}</span>
  </div>
</div>

当我console.log(groupedByLocation)时,我得到了所有应该显示的数据,但是在v-for中,我只得到了数组中的最后一个对象。

看似简单,但我真的很困惑。

任何帮助将不胜感激!

  

理想的结果是,我想在上方的一组中打印所有具有location: north的项目,并在下方的不同组中打印具有location: west的所有项目。

1 个答案:

答案 0 :(得分:0)

我不明白您为什么在for循环的每次迭代中都调用groupBy

我将通过details[0].location的{​​{1}}的{​​{3}}个filter解决问题:

continental

示例:

  methods: {
    filterByLocation: function(location) {
      return Object.values(this.continental).filter(v => v.details[0].location === location)
    }
  }
new Vue({
  el: '#app',
  data: {
    continental: {
      countryOne: {
        weather: "weatherOne",
        details: [{
          location: "west",
          foods: "foodOne",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryTwo: {
        weather: "weatherTwo",
        details: [{
          location: "north",
          foods: "foodTwo",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryThree: {
        weather: "weatherThree",
        details: [{
          location: "north",
          foods: "foodThree",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryFour: {
        weather: "WeatherFour",
        details: [{
          location: "west",
          foods: "foodFour",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      },
      countryfive: {
        weather: "WeatherFive",
        details: [{
          location: "north",
          foods: "foodFive",
          mainCities: [
            "cityOne",
            "cityTwo",
            "cityThree"
          ]
        }]
      }
    }
  },
  methods: {
    filterByLocation: function(location) {
      return Object.values(this.continental).filter(v => v.details[0].location === location)
    }
  }
})
#app {
  display: flex;
  justify-content: space-around;
  max-width: 600px;
  margin: 0 auto;
}