I know that the includes property can check for, say, if a certain string is present, but do you know how it is possible to check for whether a certain character within a string is present? As in, how would the syntax be different?
Say I have an array like: [[‘john’, ‘john-1@john.com’], [‘mike’, ‘mike@mike.com’]]
…and I only want to return the item that contains a hypen, i.e. the first item…?
Would anyone be able to assist with the following code, which does not work ? The console.logs are just for testing, but I’ve left them in.
I am trying to produce a new array containing each array item (from the original array) which contains an ‘’ (more specifically an '’ at the end of the string, but I am just trying to figure out the first part first).
I am not getting any syntax errors, but there is no output, so I must be doing something wrong with the condition test I think…
This is the code:
function searchNames(logins) {
filteredList = [];
for (let i = 0; i < logins.length; i++) {
if (logins[i.]includes("_")) {
filteredList.push(logins[i]);
}
}
console.log(filteredList);
console.log(
searchNames([
["john", "john_smith@hotmail.com"],
["mike_", "mike_@mike.com"],
])
);
}```
Do you know the name of this type of array? I mean where each array item contains two pieces of unlabelled data? By ‘unlabelled’, I mean as distinct from key value pairs…sorry, may not be using the correct language…
I am not sure that is going to work, logins[i] is an array, so includes is going to check if either of the elements inside the array are a string that contains a single underscore
what do you want exactly here?
also you should declare your filteredList with const or let
yes, thank you, I thought it was incorrect because I want to check is the array element contains a character that forms part of another string e.g. ‘tom_’ would return true…and as you say, includes is to check for a single string. Is there a similar method to check if a certain character is present or perhaps it requires looping through the array?