vue data 如何筛选得到想要的数据项?请大家帮我看看!

vue data 如何筛选得到想要的数据项?

this.products[0]

例如上面,如果无法用角标获得目标信息,需要进行一个筛选(例如 name = xxx)才可以,应该怎么处理呢?

请大家帮我看看,给我一个思路就好,非常感谢!

最佳答案

对前端数据进行筛选,一般使用 Array.filter() 函数,比如说:

var arr = [{name: "test1"}, {name: "test2"}, {name: "ask3"}];
var arr2 = arr.filter(function (v) {return v.name.indexOf('test') !== -1} );

像 Vue 里边也是,使用 computed 即可:

data() {
return {
  list: [],
  name: ""
}
},
computed: {
  filterList() {
    return this.list.filter(function (v) {return v.name.indexOf(this.name) !== -1} );
  }
}
3年前 评论
HEPING (楼主) 3年前
讨论数量: 1

对前端数据进行筛选,一般使用 Array.filter() 函数,比如说:

var arr = [{name: "test1"}, {name: "test2"}, {name: "ask3"}];
var arr2 = arr.filter(function (v) {return v.name.indexOf('test') !== -1} );

像 Vue 里边也是,使用 computed 即可:

data() {
return {
  list: [],
  name: ""
}
},
computed: {
  filterList() {
    return this.list.filter(function (v) {return v.name.indexOf(this.name) !== -1} );
  }
}
3年前 评论
HEPING (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!