2

In my Meteor app, I have an array of objects with the following structure

[{ type: "oldValue" }]

When I run

testArray[0].type = 'test'
console.log(testArray[0].type)

'test' is correctly printed in the console. However, when I run

testArray[0].type[1] = 'd'
console.log(testArray[0].type)

'test' is printed in the console instead of 'tdst'. The second letter wasn't changed to a d.

How can I change individual characters within a string?

0

3 Answers 3

4

JavaScript strings are immutable. You can't change characters in them, and assignment statements like the one in your question are just ignored. (Strings are not objects, if that's not clear; they're a primitive type, and though they seem like objects in some ways, they're not.)

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

1 Comment

Well, that's helpful and frustrating to hear...any suggestions on how I could iterate through the object and replace certain characters in the strings contained within the object? What I want to do is replace all the accented characters in the array with character codes (like "&oacute").
1

As @Pointy pointed out, strings are immutable. If you want to change certain characters, you can work with an array of characters:

var myString = 'hello there!'
myString = myString.split('')
myString[1] = 'a'
myString = myString.join('')
console.log(myString)     // 'hallo there!'

However, per your comment, you probably want to iterate through the string once, since constantly splitting and joining the string might be slow:

function replaceCharacters (str, chars, conversionFunction) {
    var charBuff = []
    for (var i = 0; i < str.length; i++) {
        if (chars.indexOf(str[i])) {
            charBuff.push(conversionFunction(str[i]))
        } else {
            charBuff.push(str[i])
        }
    }
    return charBuff.join('')
}

examples:

console.log(replaceCharacters('hello', 'al', function (myChar) { return 'Z' }))
// logs 'heZZo'
// you can pass an array or a string as the second parameter
// since `.indexOf` works on both strings and arrays

and if you're working with es6:

function replaceCharacters (str, chars, conversionFunc) {
    return [...str].reduce((p, c) => {
        p.push(chars.indexOf(c) === -1 ? c : conversionFunc(c))
        return p
    }, []).join('')
}

2 Comments

That's just what I was looking for. Thanks!
@SamAlbert I added a second method.
1

Regarding your remark, you can write your own converter or make use of the he.js library.

Regarding the latter, you can find the library here https://github.com/mathiasbynens/he

As for writing your own converter, this will give you a head start How to convert characters to HTML entities using plain JavaScript

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
Thanks for the tip, Kristjan!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.