Ayush
Ayush

Reputation: 143

How to slice every element of array in React

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?

Upvotes: 2

Views: 642

Answers (1)

Ele
Ele

Reputation: 33726

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);

Upvotes: 2

Related Questions