【发布时间】:2020-10-20 02:30:12
【问题描述】:
我想返回包含对象内数组内最近字符串的对象,在另一个数组内,
我的indexOf() 只返回确切的字符串为真。
我尝试将indexOf() 替换为march()、matches()、includes() 没有任何效果。
请指教
let SellerList = [
{ supplyList: ["apple", "orange", "red apple"] },
{ supplyList: ["apple juice", "drink", "green apple", "dream app"] },
{ supplyList: ["lamp", "dog", "cat", "man"] }
];
let stringToMatch = "app";
let filteredList = SellerList.filter(
(txt) => txt.supplyList.indexOf(stringToMatch) !== -1
);
我希望的输出是包含最近字符串的所有对象,例如:
[
{ supplyList: ["apple", "orange", "red apple"] },
{ supplyList: ["apple juice", "drink", "green apple", "dream app"] }
];
【问题讨论】:
-
来自搜索词
app,它显示了预期的结果,为什么?数组SellerList中是否有多个对象? -
@Mario 是的,我有不止一个
-
对于
stringToMatch = "app"的预期输出是什么? -
@Mario 所有包含最接近字符串的对象,例如:查看我的问题我用预期的输出示例编辑它
标签: javascript arrays string filter