如何用Startswith等分别检查一个字符串的两个单词并在Javascript中过滤
How to check two words of a string separately with Startswith etc. and filter it in Javascript
const people = [
{ first: 'John', last: 'Doe', year: 1991, continent: "North America" },
{ first: 'Jane', last: 'Doe', year: 1990, continent: "Americas" },
{ first: 'Jahn', last: 'Deo', year: 1986, continent: "Europe" },
{ first: 'Jone', last: 'Deo', year: 1992, continent: "North America" },
{ first: 'Jhan', last: 'Doe', year: 1989, continent: "Asia" },
{ first: 'Jeon', last: 'Doe', year: 1992, continent: "Europe" },
{ first: 'Janh', last: 'Edo', year: 1984, continent: "North America" },
{ first: 'Jean', last: 'Edo', year: 1981, continent: "North America"},
];
我可以用 StartsWith 检查 continent 的第一个词,但我想知道我是否在寻找 "Am" 它 returns "North America" 和 "South America"。
我现在就是这样做的;
const continent = people.filter(e =>
e.continent.startsWith("Am")
);
这个returns一个空的[]。我尝试使用 split("")
但随后它创建了一个数组并且出现错误 startswith doesn't exist in array.如果包含特定字符串,如何检查字符串中的第二个单词?最后,我只想保留数组的对象包括 "America"、"North America"、"South America".
您仍然可以使用过滤器和拆分。也使用 some
。 split
将创建一个数组,然后使用 some
查找该数组中的元素是否以所需的匹配字符开头。
例如 split
North America
将是 ['North','America']
并且 some
将检查此数组中的任何元素是否满足要求,然后它将 return true
const people = [{
first: 'John',
last: 'Doe',
year: 1991,
continent: "North America"
},
{
first: 'Jane',
last: 'Doe',
year: 1990,
continent: "Americas"
},
{
first: 'Jahn',
last: 'Deo',
year: 1986,
continent: "Europe"
},
{
first: 'Jone',
last: 'Deo',
year: 1992,
continent: "North America"
},
{
first: 'Jhan',
last: 'Doe',
year: 1989,
continent: "Asia"
},
{
first: 'Jeon',
last: 'Doe',
year: 1992,
continent: "Europe"
},
{
first: 'Janh',
last: 'Edo',
year: 1984,
continent: "North America"
},
{
first: 'Jean',
last: 'Edo',
year: 1981,
continent: "North America"
},
];
const continent = people.filter((e) => {
const continentName = e.continent.split(' ');
const startWith = continentName.some(item => item.startsWith("Am"));
if (startWith) {
return e
}
});
console.log(continent);
您可以采用正则表达式来查找具有前一个起始字符串或空格的起始词。
const
people = [{ first: 'John', last: 'Doe', year: 1991, continent: "North America" }, { first: 'Jane', last: 'Doe', year: 1990, continent: "Americas" }, { first: 'Jahn', last: 'Deo', year: 1986, continent: "Europe" }, { first: 'Jone', last: 'Deo', year: 1992, continent: "North America" }, { first: 'Jhan', last: 'Doe', year: 1989, continent: "Asia" }, { first: 'Jeon', last: 'Doe', year: 1992, continent: "Europe" }, { first: 'Janh', last: 'Edo', year: 1984, continent: "North America" }, { first: 'Jean', last: 'Edo', year: 1981, continent: "North America"}],
continent = people.filter(e => /(^|\s)Am/.test(e.continent));
console.log(continent);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以尝试这样的操作。
var filtered = people.filter(function(it) {
return it.continent.split(" ").reduce(function(a, b) {
return a || b.startsWith("Am")
}, false);
});
filtered
将包含您想要的输出。这是最接近您的原始代码的东西。
const people = [
{ first: 'John', last: 'Doe', year: 1991, continent: "North America" },
{ first: 'Jane', last: 'Doe', year: 1990, continent: "Americas" },
{ first: 'Jahn', last: 'Deo', year: 1986, continent: "Europe" },
{ first: 'Jone', last: 'Deo', year: 1992, continent: "North America" },
{ first: 'Jhan', last: 'Doe', year: 1989, continent: "Asia" },
{ first: 'Jeon', last: 'Doe', year: 1992, continent: "Europe" },
{ first: 'Janh', last: 'Edo', year: 1984, continent: "North America" },
{ first: 'Jean', last: 'Edo', year: 1981, continent: "North America"},
];
我可以用 StartsWith 检查 continent 的第一个词,但我想知道我是否在寻找 "Am" 它 returns "North America" 和 "South America"。
我现在就是这样做的;
const continent = people.filter(e =>
e.continent.startsWith("Am")
);
这个returns一个空的[]。我尝试使用 split("")
但随后它创建了一个数组并且出现错误 startswith doesn't exist in array.如果包含特定字符串,如何检查字符串中的第二个单词?最后,我只想保留数组的对象包括 "America"、"North America"、"South America".
您仍然可以使用过滤器和拆分。也使用 some
。 split
将创建一个数组,然后使用 some
查找该数组中的元素是否以所需的匹配字符开头。
例如 split
North America
将是 ['North','America']
并且 some
将检查此数组中的任何元素是否满足要求,然后它将 return true
const people = [{
first: 'John',
last: 'Doe',
year: 1991,
continent: "North America"
},
{
first: 'Jane',
last: 'Doe',
year: 1990,
continent: "Americas"
},
{
first: 'Jahn',
last: 'Deo',
year: 1986,
continent: "Europe"
},
{
first: 'Jone',
last: 'Deo',
year: 1992,
continent: "North America"
},
{
first: 'Jhan',
last: 'Doe',
year: 1989,
continent: "Asia"
},
{
first: 'Jeon',
last: 'Doe',
year: 1992,
continent: "Europe"
},
{
first: 'Janh',
last: 'Edo',
year: 1984,
continent: "North America"
},
{
first: 'Jean',
last: 'Edo',
year: 1981,
continent: "North America"
},
];
const continent = people.filter((e) => {
const continentName = e.continent.split(' ');
const startWith = continentName.some(item => item.startsWith("Am"));
if (startWith) {
return e
}
});
console.log(continent);
您可以采用正则表达式来查找具有前一个起始字符串或空格的起始词。
const
people = [{ first: 'John', last: 'Doe', year: 1991, continent: "North America" }, { first: 'Jane', last: 'Doe', year: 1990, continent: "Americas" }, { first: 'Jahn', last: 'Deo', year: 1986, continent: "Europe" }, { first: 'Jone', last: 'Deo', year: 1992, continent: "North America" }, { first: 'Jhan', last: 'Doe', year: 1989, continent: "Asia" }, { first: 'Jeon', last: 'Doe', year: 1992, continent: "Europe" }, { first: 'Janh', last: 'Edo', year: 1984, continent: "North America" }, { first: 'Jean', last: 'Edo', year: 1981, continent: "North America"}],
continent = people.filter(e => /(^|\s)Am/.test(e.continent));
console.log(continent);
.as-console-wrapper { max-height: 100% !important; top: 0; }
您可以尝试这样的操作。
var filtered = people.filter(function(it) {
return it.continent.split(" ").reduce(function(a, b) {
return a || b.startsWith("Am")
}, false);
});
filtered
将包含您想要的输出。这是最接近您的原始代码的东西。