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?

royhowie
  • 11,075
  • 14
  • 50
  • 67
Sam Albert
  • 91
  • 1
  • 1
  • 6

3 Answers3

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

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 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"). – Sam Albert Jul 06 '15 at 21:15
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('')
}
Community
  • 1
  • 1
royhowie
  • 11,075
  • 14
  • 50
  • 67
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

Community
  • 1
  • 1
Charles
  • 148
  • 1
  • 8
  • 1
    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. – Kristján Jul 07 '15 at 05:32
  • Thanks for the tip, Kristjan! – Charles Jul 07 '15 at 19:01