admin管理员组文章数量:1236698
I am using Vue.js to filter items from a JSON array, using an input field. The array consists of mobile phones.
Looking at the Samsung array, I have Samsung Galaxy S5, Samsung Galaxy S6 etc.
Now when I type: Samsung, I get a list of all the samsungs. When I type Galaxy S5, I get the Samsung Galaxy S5. But when I type Samsung S5 I get nothing, because in between 'Samsung' and 'S5' there is 'Galaxy'.
I hope someone can help me out on this one.
My code:
<input type="text" v-model="keyword" class="form-control" id="telefoon" placeholder="Search...">
<script>
var app = new Vue({
el: '#app',
data: {
keyword: '',
samsungList: []
},
computed: {
samsungFilteredList() {
return this.samsungList.filter((samsung) => {
return samsung.title.toLowerCase().includes(this.keyword.toLowerCase());
});
}
}
});
</script>
I am using Vue.js to filter items from a JSON array, using an input field. The array consists of mobile phones.
Looking at the Samsung array, I have Samsung Galaxy S5, Samsung Galaxy S6 etc.
Now when I type: Samsung, I get a list of all the samsungs. When I type Galaxy S5, I get the Samsung Galaxy S5. But when I type Samsung S5 I get nothing, because in between 'Samsung' and 'S5' there is 'Galaxy'.
I hope someone can help me out on this one.
My code:
<input type="text" v-model="keyword" class="form-control" id="telefoon" placeholder="Search...">
<script>
var app = new Vue({
el: '#app',
data: {
keyword: '',
samsungList: []
},
computed: {
samsungFilteredList() {
return this.samsungList.filter((samsung) => {
return samsung.title.toLowerCase().includes(this.keyword.toLowerCase());
});
}
}
});
</script>
Share
Improve this question
asked Nov 13, 2018 at 8:14
MxkertMxkert
3462 gold badges6 silver badges17 bronze badges
3 Answers
Reset to default 10you can split your search keyword and check, if each word exists in title with every
, which will return true, if each word will exist in title
var app = new Vue({
el: '#app',
data: {
keyword: '',
samsungList: []
},
computed: {
samsungFilteredList() {
return this.samsungList.filter((samsung) => {
return this.keyword.toLowerCase().split(' ').every(v => samsung.title.toLowerCase().includes(v));
});
}
}
});
Your requirement is partial search, For that includes
is not the correct method as it looks for exact match. You can use indexOf
let filteredResult = array.filter((i) => i.indexOf(this.keyword.toLowerCase()) > -1); // this will serve your purpose.
You can use "getMatchingResults()" method in your "samsungFilteredList " like below
var samsungList = ["Samsung Galaxy S5", "Samsung Galaxy S6"]
function getMatchingResults(input) {
return samsungList.filter(d => input.split(' ').every(v => d.toLowerCase().includes(v.toLowerCase())))
}
console.log(getMatchingResults('samsung'))
console.log(getMatchingResults('samsung s5'))
console.log(getMatchingResults('galaxy'))
本文标签: javascriptVuejs search keyword in arrayStack Overflow
版权声明:本文标题:javascript - Vue.js search keyword in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739467078a2164433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论