使用指定值筛选数组无法返回新阵列
问题描述:
我在尝试在筛选后分配新阵列时遇到问题。每次过滤时,它都会返回一个空数组。该声明适用于其他数组,但不是我想要过滤的数组。我使用Vue公司,并强调使用指定值筛选数组无法返回新阵列
// HTML
<el-button @click="searchHashtag('#hr')>Test</el-button>
// JS
data:() {
return {
// this is my array. an array of objects
messages = [{
_id = "1",
hashtags = [ "hr" ] },
{_id = "2",
hashtags = [ "hr", "#acc" ] },
{_id = "3",
hashtags = [] }]
}
methods: {
// this is suppose to return assign a new array to messages
// after it filters it's old values
searchHashtag (searchBy) {
this.messages = _.filter(this.messages,
_.compose(_.partial(_.contains, _, searchBy),
_.property('hashtags')))
我试着像这样另一个阵列功能,它给了一回:
var messages = [{
id: 1,
name: "John",
hashtag: ["#cool"]
},
{
id: 2,
name: "Bob",
hashtag: ["#cool", "#sweet"]
},
{
id: 3,
name: "Bob",
hashtag: ["#sweet"]
}
]
答
您正在搜索术语#hr但在没有任何项目您的消息数组在#标签数组中包含此项。
它们只包括小时,没有#。
这就是为什么你的过滤数组是空的。
进行了编辑。我的坏 –
你已经修复了问题中的代码,所以现在它没有任何意义。如果它有帮助,你是否能够回复问题并接受答案? –