So I have an array with strings = ["ethan" , "kathrine"]
And i have an object with multiple properties, but im only interested in the properties name
, and description
For example
arr = [
{name: "ethaniel" , description: "strong" , age: 200} ,
{name: "kathrine" , description: "beautiful" , age: 220},
{name: "zack" , description: "he loves kathrine" , age: 133},
{name: "ethan" , description: "he loves kathrine" , age: 133},
{name: "bob" , description: "he loves trucks" , age: 133}
]
The resulting array should be
arr = [
{name: "ethaniel" , description: "strong" , age: 200} ,
{name: "kathrine" , description: "beautiful" , age: 220},
{name: "zack" , description: "he loves kathrine" , age: 133},
{name: "ethan" , description: "he loves kathrine" , age: 133}
]
Because each one of them contains ethan or kathrine in the name or the description
My idea is do this
filteredArry = arr.filter(i => {return i.name.some('a') || i.description.includes('a')})
Or somethign like that
I just dont understand , if i have to use some or includes and how can i include all the content of the array that has the keywords in the include/some method
CodePudding user response:
tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
Therefore, in the filter
iterations, you need to check if any of the sub-strings is either in the name
or description
:
const
arr = [ {name: "ethaniel" , description: "strong" , age: 200} , {name: "kathrine" , description: "beautiful" , age: 220}, {name: "zack" , description: "he loves kathrine" , age: 133}, {name: "ethan" , description: "he loves kathrine" , age: 133}, {name: "bob" , description: "he loves trucks" , age: 133} ],
subStrs = ["ethan" , "kathrine"];
const filteredArry = arr.filter(({ name, description }) =>
subStrs.some(subStr => name.includes(subStr) || description.includes(subStr))
);
console.log(filteredArry);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Decomposition makes this simpler. The filter predicate is true when either name or description values includes a one of the given substrings.
// test if a person's name or description contains any substring in the substrings array
const personMatch = (personObject, substrings) => {
return includesSome(personObject.name, substrings) ||
includesSome(personObject.description, substrings)
};
// test if a string contains any substrings
const includesSome = (string, substrings) => {
return substrings.some(substring => caseInsensitiveIncludes(string, substring))
};
// maybe useful. decomposing into smaller functions lets you
// add features and retest more easily
const caseInsensitiveIncludes = (string, substring) => {
return string.toLowerCase().includes(substring.toLowerCase())
}
const people = [{
name: "ethaniel",
description: "strong",
age: 200
},
{
name: "sally",
description: "ethan is her friend",
age: 300
}
// ...
];
const names = ["Ethan", "Kathrine"];
const somePeople = people.filter(person => personMatch(person, names));
console.log(somePeople);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>