I need to filter the data based on first matching character or string of that word and print the result as below.
Input: 'T' or 'Th' or 'The' Output: ['The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight', 'Dora the explorer']
Input: 'Da' Output:['Dark Knight', 'The Dark Knight']
In the first case, 'T/Th/The' is the exact match for ('The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight'). Yes, Dora the explorer is also a valid one as 'T/Th/The' is part of the string, but I need to push it to the last.
The logic what I wrote returns ["Dora the explorer", "The Shawshank Redemption", "The Godfather", "The Godfather: Part II", "The Dark Knight"] which is not correct. Same applies for the second one too. I'm not sure what did I do wrong here.
const fruits = ['Dora the explorer', 'The Shawshank Redemption', 'The Godfather', 'The Godfather: Part II', 'The Dark Knight', 'Dark Knight'];
const character = "The";
const filteredNames = fruits.filter((item) => {
const subStrLength = character.length;
return item.split(' ').some(el => el.slice(0, subStrLength).toLowerCase() === character.toLowerCase())
}).sort((a, b) => a.toLowerCase().indexOf(character) - b.toLowerCase().indexOf(character))
console.log(filteredNames)