我需要知道如何在Vue.js中遍历数组

时间:2019-07-07 23:59:15

标签: vue.js foreach

我想用vuejs制作一个遍历对象数组的方法,并为我提供该数组中的所有名称,但是我不知道怎么做

方法将如下所示:

fruits =[
  {name: "apple", calories: "50"},
  {name: "apple", calories: "100"},
];
methode(){
  var names = ''
  foreach(fruit in this.fruits){
     names = names+friut.name
  }
  return names
}

1 个答案:

答案 0 :(得分:0)

fruits =[
  {name: "apple", calories: "50"},
  {name: "apple", calories: "100"},
];

getNameList(){
  return this.fruits.map(fruit => fruit.name)
}