Home > Software engineering >  How to slice every element of array in React
How to slice every element of array in React

Time:07-05

I have an array of elements: [AdsAyush, AdsFace, AdsKicks, AdsMac] I want to slice every element of this array (remove Ads) and get the result as [Ayush, Face, Kicks, Mac].

How can I slice the "Ads" from every element of this array.

I am doing something like this:

array.forEach(element =>
  return element.toString().slice(3);

But this is giving me error and not sure what to do?

CodePudding user response:

You can use the function Array.prototype.map as follows.

const array = ["AdsAyush", "AdsFace", "AdsKicks", "AdsMac"];
const result = array.map(element => element.slice(3));

console.log(result);

  • Related