-2

I need to check "Drinks" is already exist in this array if it exists I need to replace the new value instead of the "Drinks:Focus"

["Drinks:Focus", "Baked:No", "Addons:320"]

my angular version

@angular-devkit/architect         0.803.19 (cli-only)
@angular-devkit/build-optimizer   0.0.35
@angular-devkit/core              8.3.19 (cli-only)
@angular-devkit/schematics        8.3.19 (cli-only)
@schematics/angular               8.3.19 (cli-only)
@schematics/update                0.803.19 (cli-only)
rxjs                              5.5.6
typescript                        2.4.2
webpack                           3.12.0
  • Does this answer your question? [Check if an array of strings contains a substring in javascript](https://stackoverflow.com/questions/44440317/check-if-an-array-of-strings-contains-a-substring-in-javascript) – Heretic Monkey Apr 03 '20 at 13:00
  • The reason it was closed is because you should not expect SO users to provide a complete solution without showing what you have tried, your research, where you hare stuck. SO is not a coding service. But you got lucky, someone answered. – Nic3500 Apr 04 '20 at 23:58
  • mm thanks, @Nic3500 I didn't know about that... I tried many times but I can't find the solution that's why am submit this question... BTW it's helped me. – Sandun Wijerathne Apr 05 '20 at 16:52

1 Answers1

1

You can use Array.map() and String.includes() to achieve this, as followings:

let arr = ["Drinks:Focus", "Baked:No", "Addons:320"]

arr = arr.map(item => item.includes('Drinks') ? item.replace('Drinks','Beverage') : item)

console.log(arr)
Harun Yilmaz
  • 8,281
  • 3
  • 24
  • 35