javascript - 如何在 vue 中比较 array.include(arrayData)
问题描述
我有两个数组数据
checked: [
'orange', 'apple', 'juice'
]
和第二
products: [
'0': {
title: 'this is product title',
categories: [
'apple', 'juice'
]
}
]
我想用复选框过滤具有计算属性的数据
我试过这个
computed: {
filterProducts(){
return this.products.filter(product => this.checked.includes(product.categories));
}
}
但它不工作我该怎么做
解决方案
您正在尝试检查字符串数组是否包含数组(即使您的初始数组包含数组也不起作用)。不要使用.includes()
,而是使用.every()
onproduct.categories
来检查此数组中的所有项目是否都包含在选中的范围内:
const checked = ['orange', 'apple', 'juice'];
const products = [
{
title: 'this is product title',
categories: ['apple', 'juice']
}
];
const computed = products.filter(product => product.categories.every(item => checked.includes(item) || !checked.length));
console.log(computed);
推荐阅读
- tsql - TSQL - 计算日期
- vue.js - 当输入字段的数量不恒定时,Vue.js如何从输入字段中收集值
- javascript - Javascript POST 到 PHP 在本地工作,在外部失败
- android - 先前的活动在未完成退出转换的情况下完成
- selenium - 无法在 Windows 上使用 WebdriverIO 启动 Selenium 服务器
- vb.net - 搜索包含字符串访问数据库的字符串
- sql - 当前行和下一行之间的 DateDiff >= 10 天
- php - 如何在一段时间(24 小时)内阻止 IP 地址
- java - 重复号码检查并不总是有效
- c - c中同一结构的多个名称