return 所有数组如果至少有一个元素匹配
return all array if at least one element match
使用以下数组数组我需要搜索一个元素,如果找到该元素,它应该return它所属的整个数组
例子
const data= [[
"car",
"plane",
"boat"
],
[
"paris",
"barcelona",
"los angeles"
],
[
"milk",
"eggs",
"brea",
]
]
如果配车必须return
[plane, boat]
如果匹配巴黎必须return
[barcelona, los angeles]
Find/Filter 将 return 数组
const data= [
[ "car", "plane", "boat" ],
[ "paris", "barcelona", "los angeles"],
[ "milk", "eggs", "bread" ]
]
const findArr = (nestedArr, item) => nestedArr
.find(arr => arr.includes(item))
const findAndExcludeItem = (nestedArr, item) => {
const arrContaining = nestedArr.find(arr => arr.includes(item)) || []; // in case not found
return arrContaining.filter(filteredItem => filteredItem != item);
};
const excludeArr = (nestedArr, item) => nestedArr
.filter(arr => !arr.includes(item))
console.log(findArr(data,"paris"))
console.log(findAndExcludeItem(data,"paris"))
console.log(excludeArr(data,"paris"))
使用以下数组数组我需要搜索一个元素,如果找到该元素,它应该return它所属的整个数组
例子
const data= [[
"car",
"plane",
"boat"
],
[
"paris",
"barcelona",
"los angeles"
],
[
"milk",
"eggs",
"brea",
]
]
如果配车必须return
[plane, boat]
如果匹配巴黎必须return
[barcelona, los angeles]
Find/Filter 将 return 数组
const data= [
[ "car", "plane", "boat" ],
[ "paris", "barcelona", "los angeles"],
[ "milk", "eggs", "bread" ]
]
const findArr = (nestedArr, item) => nestedArr
.find(arr => arr.includes(item))
const findAndExcludeItem = (nestedArr, item) => {
const arrContaining = nestedArr.find(arr => arr.includes(item)) || []; // in case not found
return arrContaining.filter(filteredItem => filteredItem != item);
};
const excludeArr = (nestedArr, item) => nestedArr
.filter(arr => !arr.includes(item))
console.log(findArr(data,"paris"))
console.log(findAndExcludeItem(data,"paris"))
console.log(excludeArr(data,"paris"))