【发布时间】:2019-11-11 12:36:39
【问题描述】:
我的方法获得了 90 美分的成功,但是当响应在其中一个子键中有多个条目时,逻辑就会失败,我无法放置一个适用于所有情况的通用逻辑.
响应样本是
{
"items": [
{
"id":1,
"name": "John",
"sections": [
{
"id":1,
"description": "John smith"
}
]
}
]
}
现在我的用例说您搜索 John 文本,然后 items 数组将包含许多对象,其 items.name 或 items.sections.description 应包含“John”关键字
我输入的匹配逻辑运行良好,因为我正在遍历 items[].name 和 items.sections[].description
主要挑战来自sections[*].description 包含多个部分,如下所示
{
"items": [
{
"id":1,
"name": "John",
"sections": [
{
"id":1,
"description": "John smith"
},
{
"id":1,
"description": "remain smith of the first object"
}
]
}
]
}
逻辑现在应该可以运行了 items[].name 或 items.sections[].description(section[*].description 的多个条目)
我面临的问题是当我迭代 items[].name & items[].sections[*].description
它给了我所有的名字和所有的sections.description在单独的数组中我想要的是它应该一个一个地给我。
例如第一个结果集应该在下面给我
[
"John"
]
and
[
"John smith"
"remain smith of the first object"
]
这样我就可以运行现有的逻辑来检查 John 是否可用。目前我的逻辑在描述的第一个条目上运行,它不检查下一个条目或 section.description 这是匹配对象失败的原因,因为描述的第二个条目中存在“john”
{
"items": [
{
"id":11,
"name": "SMITH",
"sections": [
{
"id":11,
"description": "SMITH"
},
{
"id":11,
"description": "JOHN Carter"
}
]
}
]
}
我目前使用的匹配逻辑是——
* def matchText =
"""
function (nameArr, sectionArr, matchingWord)
{
for(var i = 0; i < nameArr.length; i++)
var regEx = new RegExp(matchingWord, 'gi')
var nameMatch = nameArr[i].match(regEx)
var secMatch = sectionArr[i].match(regEx)
if (nameMatch ==null && secMatch == null) {
return false;
}
}
return true;
}
"""
* def getName = get response.items[*].name
* def getDescription = get response.items[*].sections[*].description
* assert matchText(getName,getDescription,'john')
因此,当您在 name 和 section.description 中具有相同长度但 section.description 有多个数组时,此逻辑有效,则它无法正确迭代。这是我想将名称视为一个对象而将sections.description 视为另一个对象的唯一原因,即使其中会有多个子ID。
【问题讨论】: