1

I have a string array with strings like age ,gender,nationality. Need to replace age to agexxxx. So is there any method to replace a string in string array on angular?

this.segment=['age','Nationality','gender'];
1
  • Try this ['age','Nationality','gender'].map(w => w === 'age' ? 'agexxxx' : w); If you want to modify the existing array, you can use forEach instead of map. Commented Feb 9, 2018 at 5:27

3 Answers 3

5

Here you go :

var segment=['age','Nationality','gender'];

var changed = segment.map(seg => seg.replace('age', 'agexx') );


console.log('Original --->' ,segment);
console.log('Changed --->' ,changed);

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

1 Comment

@Vickie , Glad to know , Happy Coding :)
0

Use:

for(var i=0; i < this.segment.length; i++) {
 this.segment[i] = this.segment[i].replace('age', 'agexx');
}

Comments

0

let a = ['age','hello','world']
a.splice(0, 1,'agexxx');
alert(a)

reference : Array.prototype.splice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.