【发布时间】:2019-01-21 09:34:29
【问题描述】:
我有大量直接来自数据库的数据,这些数据存储在 JS 对象中:{},但是我无法使用内置 JS 的 .filter 函数。 在我使用 Vue.JS 时避免使用箭头函数。
JS 对象示例:
0:
brand: "Brand A"
compatibilitySetIds:Array(3)
createdAt:"2018-08-13T09:07:50.772Z"
deletedAt:null
id:"e7915261-677d-4527-90c6-09b5170afca8"
model:"Model-1"
series:null
type:"A"
updatedAt:"2018-08-13T09:07:50.772Z"
1:
brand: "Brand B"
compatibilitySetIds:Array(3)
createdAt:"2018-08-13T09:07:50.772Z"
deletedAt:null
id:"e7915261-677d-4527-90c6-09b5170afca8"
model:"Model-51"
series:"S02"
type:"B"
updatedAt:"2018-08-13T09:07:50.772Z"
我需要能够:
- 按型号过滤
- 最好也按品牌/系列/类型过滤
- 能够在所有字段中按 asc/desc 排序
我目前拥有的(作品)
computed: {
filteredEquipment: function() {
if (this.search.selected === '' || !this.search.selected) {
return this.equipmentItems;
} else {
const model = this.search.selected;
console.log(model);
const filtered = this.equipmentItems.filter(function(item) {
return item.model === model;
});
return filtered;
}
},
},
附言。 使用 Vue-JS 和 TypeScript。
PPS。 我必须在客户端进行过滤,而不是在服务器端。
【问题讨论】:
-
请提供您正在使用的数据对象的更好示例。
-
如果它们的索引是 0 和 1,为什么不将这些对象存储在数组中?
-
您的示例对象不是有效的 JS。
-
当你说你“无法使用内置 JS 的 .filter 函数”时,你的意思是你根本不在一个支持它的环境中?或者您是说使用
.filter()无法使其工作? -
你的叙述说,“避免使用箭头函数,因为我正在使用 Vue.JS。”...但是在你目前拥有的示例中,你正在使用箭头函数。
标签: javascript typescript vue.js vuejs2