2

I am checking if a string contains one or more entries from an array, using .some() and .includes() as you can see below.

If the string does include one or more entries from the array, I would like to know which one(s). I haven't found a way to achieve this so far.

Here is my code:

var hobby = ["sport","art","music"];
var message = "I love sport!";

if(hobby.some(el => message.includes(el))){
    console.log("a hobby is included");

     // tell me which hobby or hobbies here
}
0

3 Answers 3

2

Use .filter instead of .some:

var hobby = ["sport", "art", "music"];
const checkMessage = (message) => {
  const hobbies = hobby.filter(word => message.includes(word));
  if (hobbies.length !== 0) {
    console.log('Looks like you like ' + hobbies.join(','));
  } else {
    console.log('No matching hobbies found');
  }
};

checkMessage("I love sport!");
checkMessage("foo bar");
checkMessage("music and art");

If you wanted the matching indicies, use .reduce:

var hobby = ["sport", "art", "music"];
const checkMessage = (message) => {
  const hobbyIndicies = hobby.reduce((a, word, i) => {
    if (message.includes(word)) {
      a.push(i);
    }
    return a;
  }, []);
  if (hobbyIndicies.length !== 0) {
    console.log('Looks like you like indicies ' + hobbyIndicies.join(','));
  } else {
    console.log('No matching hobbies found');
  }
};

checkMessage("I love sport!");
checkMessage("foo bar");
checkMessage("music and art");

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, thank you! Just curious, instead of using join(), is there a simple way to get the indexes of the matching words from the first array? For example here "0" for sport in array "hobby"?
Use .reduce instead, see edit, though that's pretty weird, I'd strongly prefer .filter
2

The .filter() method can be used in place of .some() to acquire a list of items that are included in the message string. This can in turn be used to display the hobbies in the message.

The logic would involve an additional check to ensure that the length of the filtered list is not empty before logging the result to console:

var hobby = ["sport", "art", "music"];
var message = "I love sport!";

// Use .filter instead of .some
var result = hobby.filter(el => message.includes(el));

// If result not empty, log the result to console
if (result.length > 0) {
  console.log("a hobby or hobbies are included:", result);

  // To find matching indicies
  const matchingIndicies = hobby
    .map((str, index) => ({ str, index }))
    .filter((item) => message.includes(item.str))
    .map((item) => (item.str + ' at index ' + item.index));
 
  console.log("matching indicies:", matchingIndicies)
  // tell me which hobby or hobbies here
}

Comments

0

I prefer a simple forEach loop like the following:

var hobby = ["sport", "art", "music"];
var message = "I love sport!";
var idxs = []
var vals =[]
hobby.forEach(function(val, idx){
  if (message.includes(val))
  {
      idxs.push(idx);
      vals.push(val)
  }
});

if (vals.length !== 0) {
  console.log('You like ' + vals.join(','));
  console.log('Your hobby indices: ' + idxs.join(','));
} else {
  console.log('You Like Nothing!!');
}

Comments

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.