arrays - 在Vue中使用计算函数基于多维数组过滤数组时遇到问题
问题描述
使用 vue 我试图更改/更新一个数组(draggables),其逻辑是如果对象存在于另一个数组数组(drozones)中,它将从draggables中删除
每个可拖动对象只能在与可拖动对象 dropID 匹配的多维数组的索引中找到。
我尝试过的每种方法要么返回所有可拖动项目,要么返回所有可拖动项目,要么删除不正确的可拖动项目。
// In this example I have three droppable and two drop areas, the first drop area can handle two droppable and the last area only one.
<div id="app">
<ul>
<li v-for="item in draggables">Item ID: {{item.id}} - Dropzone ID: {{item.dropID}}</li>
</ul>
</div>
data: {
draggableItems: [{id: 0, dropID: 0 }, {id: 1, dropID: 0 }, {id: 2, dropID: 1 }],
dropzones: [[{id: 1, dropID: 0 }], []]
},
computed : () {
draggables () {
return this.draggableItems.filter((item, index) => {
console.log('item', item)
return this.dropzones.filter((zone,i) => {
if (zone.length === 0) return
return item.id !== zone[i].id
})
})
}
}
这是我的一些尝试,但还没有运气。谢谢你的帮助
解决方案
您需要检测比较 2 个对象的键,或者您需要一个函数来比较对象。您可以尝试isEqual
功能lodash
或比较 JSON.stringify 的 2 个结果
new Vue({
el: '#app',
data: {
draggableItems: [{id: 0, dropID: 0 }, {id: 1, dropID: 0 }],
dropzones: [
[
{id: 1, dropID: 0 }
],
[]
]
},
computed: {
draggables () {
return this.draggableItems.filter((item, index) => {
return !this.dropzones.some((zone,i) => {
return zone.some(zoneitem => JSON.stringify(item) === JSON.stringify(zoneitem))
})
})
}
}
})
</script>
推荐阅读
- wpf - 触控笔/笔离开屏幕时的 WPF 触控笔事件
- quartz-scheduler - Quartz Custom Logger:消息函数为空
- jquery - Jquery延迟和重定向
- javascript - Vue 数据对象在被 @change 触发时没有反应
- python - F1分数指标和分类报告sklearn的F1分数值不同
- sql-server - 要创建什么类型的索引?
- sql - 如何将数据库中某些表的所有权从 postgres 更改为另一个用户?
- python - python matplotlib 中的图例键(标记)是否有 y 偏移?
- javascript - 迭代定义为 Promise 的嵌套对象数组
- magento2 - Magento 2 - 用于混合的混合