简体   繁体   English

如何在Javascript数组中查找单词?

[英]How to find a word in Javascript array?

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"}
]

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')); 

Use .filter : 使用.filter

 const input=[{name:"Jeff Crawford",tel:"57285"},{name:"Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}] const filtered = input.filter(({ name }) => name.startsWith('Jeff')); console.log(filtered); 

If you want to check to see if "Jeff" is anywhere rather than only at the beginning, use .includes instead: 如果要检查“ Jeff”是否在任何地方而不是仅在开头,请改用.includes

 const input=[{name:"Jeff Crawford",tel:"57285"},{name:"foo-Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}] const filtered = input.filter(({ name }) => name.includes('Jeff')); console.log(filtered); 

These are ES6 features. 这些是ES6功能。 For ES5, use indexOf instead: 对于ES5,请改用indexOf

 var input=[{name:"Jeff Crawford",tel:"57285"},{name:"foo-Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}] var filtered = input.filter(function(obj){ return obj.name.indexOf('Jeff') !== -1; }); console.log(filtered); 

Use Array#map on the original array and return the indices. 在原始数组上使用Array#map并返回索引。 Then filter out the undefined values: 然后过滤掉 undefined值:

 const data = [{name:"Jeff Crawford",tel:"57285"},{name:"Jeff Maier",tel:"52141"},{name:"Tim Maier",tel:"73246"}]; const indices = data.map((item, i) => { if (item.name.startsWith('Jeff')) { return i; } }) .filter(item => item !== undefined); console.log(indices); 

Use array filter method along with indexOf . indexOf一起使用数组filter方法。 filter will return a new array of matched element. filter将返回匹配元素的新数组。 In side the filter callback function check for the name where the indexOf the keyword is not -1 . 在过滤器回调函数的旁边,检查indexOf关键字不是-1 That is the name should contain the keyword 那是名字应该包含关键字

 var originalArray = [{ name: "Jeff Crawford", tel: "57285" }, { name: "Jeff Maier", tel: "52141" }, { name: "Tim Maier", tel: "73246" } ] function getMatchedElem(keyWord) { return originalArray.filter(function(item) { return item.name.indexOf(keyWord) !== -1 }) } console.log(getMatchedElem('Jeff')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM