Convert JavaScript array iteration result into a single line text string


Let’s say, we have a string and an array −

const textString = 'Convert javascript array iteration result into a
single line text string. Happy searching!';
const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam'];

We have to write a function that maps the array to a string containing only true and false depending on the fact whether the corresponding array element is present in the string or not.

Example

const textString = 'Convert javascript array iteration result into a
single line text string. Happy searching!';
const keywords = ['integer', 'javascript', 'dry', 'Happy', 'exam'];
const includesString = (arr, str) => {
   return arr.reduce((acc, val) => {
      return acc.concat(str.includes(val));
   }, []).join(', ');
};
console.log(includesString(keywords, textString));

Output

The output in the console will be −

false, true, false, true, false

Updated on: 21-Aug-2020

172 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements