I want to write a function that checks if an object has at least one value containing a substring. Something like this (pseudocode):
const userMatchesText = (text, user) => user.includes(text);
The full structure of my objects (
So for the user as shown below:
const user = {
id: '123abc',
info: {
age: 12,
bio: 'This is my bio'
},
social: {
chatName: 'Chris',
friends: ['friend1', 'other friend'],
blocks: ['Creep']
}
}
,
userMatches('bi', user)Should return true, because the substring "bi" is located in the bio: "This is my bio". userMatches ('324d, user) should also return false. usermatches('blocks', user)should return false, because the substring is in only one of the keys, and not in one of the values.
The objects I'm working with look like this (Mongoose schema):
{
account : {
dateOfCreation : Number
},
social : {
chatName : String,
friends : [ { type: String } ],
blocks : [ { type: String } ],
sentRequests : [ { type: String } ],
recievedRequests : [ { type: String } ],
threads : [ { type: String } ]
},
info : {
age : Number,
bio : String,
displayName : String,
profilePicture : String,
subjects : {
tutor : [
{
subject : String,
level : String
}
],
student : [
{
subject : String,
level : String
}
]
}
},
facebook : {
id : String,
firstName : String,
middleName : String,
fullName : String,
lastName : String
}
}
, , - , , map includes, .
const doesUserMatchText = (user, text) => {
const { social: { chatName }, info: { displayName }, facebook: { firstName, middleName, lastName } } = user;
const possibleMatches = [ chatName, displayName, firstName, middleName, lastName ];
let match = false;
possibleMatches.map(possibleMatch => {
if (possibleMatch.includes(text)) {
return (match = true);
}
});
};
, , (, , ), , , . , userMatchesText(text, user) . !
, , , . - , , , , , .., "".