How to search in an array inside the array of the object by one single character
const arr = [{ x: 1, tags: ["tag1", "taf", "ee", "xx"] },{ x: 3, tags: ["ta", "e", "xx"] }];
const fResult = arr.filter((res) => res.tags().includes("tag1"));
this will return the value if we input a full tag1 word, not just "t" or "ta"
CodePudding user response:
If you want to match that the value starts with it then you want to use some()
const filterIt = (arr, text) => arr.filter(
(res) =>
res.tags.some(
val => val.startsWith(text)
)
);
const arr = [{
x: 1,
tags: ["tag1", "taf", "ee", "xx"]
}, {
x: 3,
tags: ["ta", "e", "xx"]
}];
console.log("t", filterIt(arr, "t"));
console.log("ta", filterIt(arr, "ta"));
console.log("tag", filterIt(arr, "tag"));
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
finally figured out this simple solution
by converting the array of tags to string, then checking by including is solve everything
const arr = [{ x: 1, tags: ["tag1", "taf", "ee", "xx"] },{ x: 3, tags: ["ta", "e", "xx"] }];
here is the solution const fResult = arr.filter((res) => res.tags.toString().includes("tag1"));