本文介绍了如何在Javascript数组中查找单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是,如何才能通过单词找到所有数组索引?
My question is, how can I find all array indexes by a word ?
[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
{name: "Tim Maier", tel: "73246"}
]
如果我搜索"Jeff",我想得到:
If I search for "Jeff", I want to get:
[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
]
推荐答案
要使其更具通用性,您可以采用一个函数,该函数采用对象数组,所需的键和搜索字符串,稍后将其用作小写字母字符串.
To make it more versatile, you could take a function which takes an array of objects, the wanted key and the search string, wich is later used as lower case string.
function find(array, key, value) {
value = value.toLowerCase();
return array.filter(o => o[key].toLowerCase().includes(value));
}
var array = [{ name: "Jeff Crawford", tel: "57285" }, { name: "Jeff Maier", tel: "52141" }, { name: "Tim Maier", tel: "73246" }]
console.log(find(array, 'name', 'Jeff'));
这篇关于如何在Javascript数组中查找单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!