如何检查 Javascript 数组项的字符串中是否有两个(或更多)正斜杠
How to check if there are two (or more) forward slashes in a string of an Array item with Javascript
我有以下来自 graphql 查询的响应:
"menu": [{
"url": ""
},
{
"url": "/"
},
{
"url": "/item/child-item"
}
]
我需要一个布尔变量并检查数组中是否有一个项目带有两个(或更多)正斜杠 /
。所以它在路径中有一个子项,如 "/item/child-item"
const hasChild = data.menu.map((item) => item.url.includes('// What to do here?'));
你不需要map()
。 map()
是当您想使用函数将数组转换为另一个数组时。
您需要 some()
,如果数组中的任何一项满足条件,则 returns 为真。
您可以将它与 split()
一起使用来计算字符串中特定字符的出现次数。
const data = { "menu": [
{
"url": ""
},
{
"url": "/"
},
{
"url": "/item/child-item"
}
] }
const hasChild = data.menu.some((item) => item.url.split('/').length - 1 > 1);
console.log(hasChild);
在上面的代码中 split()
,
/a/b/c
变为数组 ['','a','b','c']。所以你总是得到 /
+ 1.
的数字
.map()
method returns an array, as you're after a boolean, you need to use something different. The .some()
method can instead be used, which will return true
as soon as the callback you pass it returns true
. The .some()
will iterate through all of your objects within your array until your callback returns true
. You can use the .replace()
方法删除任何非 /
字符的出现并抓取长度以检查它是否出现两个或更多:
const hasChild = data.menu.some(menuItem => menuItem.url.replace(/[^/]/g, '').length >= 2);
参见下面的可运行示例:
const data = {
"menu": [{
"url": ""
},
{
"url": "/"
},
{
"url": "/item/child-item"
}
]
};
const hasChild = data.menu.some(menuItem => menuItem.url.replace(/[^/]/g, '').length >= 2);
console.log(hasChild); // true
您可以使用 Array#some
,至于测试,您可以将 url
拆分为 /
,如果生成的数组包含 2 个以上的元素,则至少有字符串中的两个 /
.
const input = {"menu": [ { "url": "" }, { "url": "/" }, { "url": "/item/child-item" } ] },
output = input.menu.some(({url}) => url.split("/").length > 2);
console.log( output );
我有以下来自 graphql 查询的响应:
"menu": [{
"url": ""
},
{
"url": "/"
},
{
"url": "/item/child-item"
}
]
我需要一个布尔变量并检查数组中是否有一个项目带有两个(或更多)正斜杠 /
。所以它在路径中有一个子项,如 "/item/child-item"
const hasChild = data.menu.map((item) => item.url.includes('// What to do here?'));
你不需要map()
。 map()
是当您想使用函数将数组转换为另一个数组时。
您需要 some()
,如果数组中的任何一项满足条件,则 returns 为真。
您可以将它与 split()
一起使用来计算字符串中特定字符的出现次数。
const data = { "menu": [
{
"url": ""
},
{
"url": "/"
},
{
"url": "/item/child-item"
}
] }
const hasChild = data.menu.some((item) => item.url.split('/').length - 1 > 1);
console.log(hasChild);
在上面的代码中 split()
,
/a/b/c
变为数组 ['','a','b','c']。所以你总是得到 /
+ 1.
.map()
method returns an array, as you're after a boolean, you need to use something different. The .some()
method can instead be used, which will return true
as soon as the callback you pass it returns true
. The .some()
will iterate through all of your objects within your array until your callback returns true
. You can use the .replace()
方法删除任何非 /
字符的出现并抓取长度以检查它是否出现两个或更多:
const hasChild = data.menu.some(menuItem => menuItem.url.replace(/[^/]/g, '').length >= 2);
参见下面的可运行示例:
const data = {
"menu": [{
"url": ""
},
{
"url": "/"
},
{
"url": "/item/child-item"
}
]
};
const hasChild = data.menu.some(menuItem => menuItem.url.replace(/[^/]/g, '').length >= 2);
console.log(hasChild); // true
您可以使用 Array#some
,至于测试,您可以将 url
拆分为 /
,如果生成的数组包含 2 个以上的元素,则至少有字符串中的两个 /
.
const input = {"menu": [ { "url": "" }, { "url": "/" }, { "url": "/item/child-item" } ] },
output = input.menu.some(({url}) => url.split("/").length > 2);
console.log( output );