【发布时间】:2019-05-09 15:41:23
【问题描述】:
我收到一个返回的响应和一个哈希数组。哈希数组有两个键“title”和“paragraph”。有时我会收到在段落键中返回相似值的响应。
例如当我只返回段落中的值时:
["Welcome to the best place", "Welcome to the best place in the world, Boston!"]
您会看到索引 0 处包含索引 1 处的内容
我正在映射哈希数组以返回其中一个键“段落”。然后,如果该值等于数组中的任何其他元素,我会尝试过滤掉第一个元素。我有一些东西只有在数组具有与上述状态相似的值时才有效,如果失败将返回一个空数组。
const description = hotel
.description()
.map(descriptions => descriptions.paragraph)
.filter((paragraph, index) => !paragraph[index].includes(paragraph[0]))
其中hotel.description() 返回哈希数组,要过滤的映射链将返回数组中的结果
上面的示例代码返回一个有效的响应,其中数组:
["Welcome to the best place", "Welcome to the best place in the world, Boston!"]
变成:
["Welcome to the best place in the world, Boston!"]
但如果返回的数组是唯一的,则返回一个空数组。
预期结果是:
["You are here at the best place", "Welcome to the best place in the world, Boston!"]
实际结果是:
[]
不确定要在该链上附加什么以使其返回唯一值。
【问题讨论】:
-
你想得到所有在标题和段落中常见的词吗?
-
@MurtazaHussain 如果所有值都是唯一的,我想返回段落的数组,并过滤掉长度相似的值。
-
我不得不多想一下如何实现你想要的,但我认为你的代码失败的原因是
paragraph[index]和paragraph[0]是你当前段落中的字符,不是段落列表中的条目。 -
所以对于 [ABC, AB, DEF, DEFG] 你期待 [ABC, DEFG]?同样使用段落 [0] 确实没有什么意义。
-
@user1514042 没错。