如何在 Javascript 数组中查找单词?
How to find a word in Javascript array?
我的问题是,如何通过一个词找到所有数组索引?
[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
{name: "Tim Maier", tel: "73246"}
]
如果我搜索 "Jeff",我想得到:
[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
]
使用.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);
如果您想检查 "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);
这些是 ES6 的特性。对于 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);
使用Array#map
on the original array and return the indices. Then filter out 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);
使用数组 filter
方法和 indexOf
。 filter
将 return 一个新的匹配元素数组。在过滤器回调函数中检查 indexOf
关键字不是 -1
的名称。即名称中应包含关键字
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'))
为了使其更加通用,您可以采用一个函数,该函数采用对象数组、所需的键和搜索字符串,稍后用作小写字符串。
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'));
我的问题是,如何通过一个词找到所有数组索引?
[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
{name: "Tim Maier", tel: "73246"}
]
如果我搜索 "Jeff",我想得到:
[
{name: "Jeff Crawford", tel: "57285"},
{name: "Jeff Maier", tel: "52141"},
]
使用.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);
如果您想检查 "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);
这些是 ES6 的特性。对于 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);
使用Array#map
on the original array and return the indices. Then filter out 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);
使用数组 filter
方法和 indexOf
。 filter
将 return 一个新的匹配元素数组。在过滤器回调函数中检查 indexOf
关键字不是 -1
的名称。即名称中应包含关键字
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'))
为了使其更加通用,您可以采用一个函数,该函数采用对象数组、所需的键和搜索字符串,稍后用作小写字符串。
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'));