Convert JavaScript array iteration result into a single line text string

JavascriptWeb DevelopmentObject Oriented Programming

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
raja
Published on 21-Aug-2020 14:15:06
Advertisements