Filter method/unable to read properties of 'includes'

Hi everyone,

I am getting the following error message for the code below:

Uncaught TypeError: Cannot read properties of undefined (reading ‘includes’).

Would someone know what I’m doing wrong please?

function searchNames(logins) {
  let filteredList = logins.filter((item) => {
    return item.name.includes("_");
  });
  console.log(filteredList);
}

Error means whatever is passed as the item, it doesn’t have the name property. So item.name is undefined and undefined doesn’t have includes method.

many thanks

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…?

includes will work with any length of searched string.

1 Like

those are arrays, you can check the one you want with bracket notation and the index

1 Like

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…

you want the dot outside the square brackets

it’s an array of arrays

1 Like

thanks, I actually have it like that in VS code, don’t know why I have it like that here. It’s still not working though?

what’s your latest 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"],
    ])
  );
}

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?

you need to check both items of the array

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.