【发布时间】:2020-07-09 13:07:58
【问题描述】:
我有三个模型(我在前端使用 Vue/Vuex-ORM)。 Category、CategoryItem 和 Item。
我能够获取一个类别数组,每个类别中都有一个项目数组。中间连接模型定义了这两个模型的关系,我可以这样访问:
// array of categories, each category has array of items
const categories = Category.query().where('pack_id', this.selectedPack.id).with('items').get();
categories.map(category => {
category.items.forEach(item => {
console.log('item.pivot: ', item.pivot); // pivot refers to join model
// how to order items based on item.pivot?
})
})
在.forEach 中,我可以使用item.pivot 访问连接模型。然而,我想做的是根据item.pivot.position 对每个类别的项目进行排序。
我开始沿着 .map 内的第一行定义一个新的空数组的路径,然后理论上会根据位置是更高还是更低推入一个新值,但我不能完全围绕如何实现这一点。
谢谢!
【问题讨论】:
标签: javascript arrays vue.js foreach vuex-orm