-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
3
  • Does this answer your question? Check if an array of strings contains a substring in javascript Commented Apr 3, 2020 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. Commented Apr 4, 2020 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. Commented Apr 5, 2020 at 16:52

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.