1

Hello i'm working on dynamic search for my first web application and i have a huge problem i've been working on this for several hours using diffrent solutions in the end i think this might be the closest to resolving my problems.

Search is not static so hard coding this is not an option.

const search = 'someone loves some thing';
const teams = ['some thing', 'someone', 'help'];
const twoTeams = [];

if (teams.some(el => search.includes(el))) {
  teams.forEach(word => {
    if (search.includes(word)) {
      twoTeams.push(word);
    }
  })
}
console.log(twoTeams); // ['some thing','someone']
console.log(search) // 'someone loves some thing'
// looking for // console.log(twoTeams)// 'someone','some thing'

And here im stuck i have array of items that i need to split string with to access data from API i just need it in that order i cant reverse order because in the app theres too many elements in array and user can search anything so you dont know which one should will be index[0] and which one should be index[1] and thats crucial to my dynamic search.

2
  • so what is your desired output? Commented Jul 15, 2020 at 6:12
  • @KarlL im looking for if search is 'someone loves some thing' output has to be : 'someone','some thing' Commented Jul 15, 2020 at 6:20

2 Answers 2

2

First of all, I'd start with removing the outer if as we check every word anyway in the inner condition.

teams.forEach(word => {
    // Get the position of word in the search text. Negative value means that there is no match.
    let index = search.indexOf(word);

    // If the search text contains the word
    if (index >= 0) {
        // Save both the matching word and its index
        twoTeams.push( { 'word': word, 'index': index});
    }
})

// Sort the results by the index and select the word only
twoTeams = results.sort((a,b)=> a.index - b.index).map(result => result.word);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your time, been working on this for 9 hours i had no idea that sort method exist, your answer was incredibly helpful.
This answer would be more efficient than mine. It would find in search only once. +1.
0

You can simplify your code to built twoTeams object with filter like below.

const twoTeams = teams.filter(el => search.includes(el));

As you want preserve order then you can sort your results with indexOf like below.

twoTeams.sort((a, b) => search.indexOf(a) - search.indexOf(b));

Try it below.

const search = 'someone loves some thing';
const teams = ['some thing', 'someone', 'help'];
const twoTeams = teams.filter(el => search.includes(el));
twoTeams.sort((a, b) => search.indexOf(a) - search.indexOf(b));

console.log(twoTeams); // looking for ['someone', 'some thing']
console.log(search) // 'someone loves some thing'    

1 Comment

Yeah, i overcomplicated that a bit :D, lost track after several hour's this is a great and clever answer. It's just mad that that i spend whole night trying to resolve this and i was 4 lines of code. Thank you for help !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.