Home > Mobile >  How can i match a string with another string and return true for it?
How can i match a string with another string and return true for it?

Time:03-23

Well this is my code where m matching a string with username passed and getting result from Api and updating a property isSelected to true if it gets matched but it is not returning true for the same

const userFilter = [{
  userName: 'A Roy'
}, {
  userName: 'John Doe'
}];

const userName = 'JoHN Doe, A Roy';

userFilter.forEach(a => {
  console.log(a.userName.toLowerCase().includes(userName.toLowerCase()));
});

CodePudding user response:

Your logic is wrong. In your code, you're checking whether the userName of the array elements contain a string. Apparently, you want to check whether the string contains the userName of the array elements:

const userFilter = [{
  userName: 'A Roy'
}, {
  userName: 'John Doe'
}];

const userName = 'JoHN Doe, A Roy';

userFilter.forEach(a => {
  console.log(userName.toLowerCase().includes(a.userName.toLowerCase()));
});

userFilter.forEach(a => {
  a.isSelected = userName.toLowerCase().includes(a.userName.toLowerCase());
  a.name = a.userName;
  a.label = a.name;
  a.id = a.UserId;
});
console.log(userFilter);

  • Related