# Replace string by schema inside array

**URL:** <https://discourse.nodered.org/t/replace-string-by-schema-inside-array/57199>\
**Category:** General\
**Created:** [25 January 2022 10:49 UTC](https://discourse.nodered.org/t/replace-string-by-schema-inside-array/57199 "2022-01-25T10:49:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ketpa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ketpa/32/42496_2.png) [@ketpa](https://discourse.nodered.org/u/ketpa)\
**Post date:** [25 January 2022 10:49 UTC](https://discourse.nodered.org/t/replace-string-by-schema-inside-array/57199/1 "2022-01-25T10:49:31Z")

</div>

I want to change myString by mobiles schema.  
myString = "Samsung, Google, Apple ";  
myNewString = 'Galaxy Note 9, Pixel 3, iPhone X'  
How to do that?

```auto
const mobiles = [
    {
        oldWord: 'Samsung',
        newWord: 'Galaxy Note 9'
    },
    {
        oldWord: 'Google',
        newWord: 'Pixel 3'
    },
    {
        oldWord: 'Apple',
        newWord: 'iPhone X'
    }
];

var myString = "Samsung, Google, Apple ";

```

I try something like this, but I can't understood what is wrong in my code.

```auto
[{"id":"3179b52506f8de37","type":"function","z":"43fcf7fcdce59eff","name":"","func":"const mobiles = [\n {\n oldWord: 'Samsung',\n newWord: 'Galaxy Note 9'\n },\n {\n oldWord: 'Google',\n newWord: 'Pixel 3'\n },\n {\n oldWord: 'Apple',\n newWord: 'iPhone X'\n }\n];\n\nvar myString = \"Samsung, Google, Apple \";\nvar reg = new RegExp(oldWord, \"gi\");\n\nvar oldWord = '';\nvar newWord = '';\n \nmyString = myString.replace(reg, newWord);\nvar myNewString = '';\n \nmobiles.forEach(mobile => {\n for (let key in mobile) {\n // console.log(`${key}: ${mobile[key]}`);\n oldWord = key;\n newWord = mobile[key];\n myNewString = myString.replace(reg, newWord);\n node.send({\n myString: myString,\n myNewString: myNewString\n \n\n });\n }\n});\n\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":300,"y":560,"wires":[["dc4ef151051e87b6"]]},{"id":"d525d7417bf00397","type":"inject","z":"43fcf7fcdce59eff","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":560,"wires":[["3179b52506f8de37"]]},{"id":"dc4ef151051e87b6","type":"debug","z":"43fcf7fcdce59eff","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":470,"y":560,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 January 2022 11:31 UTC](https://discourse.nodered.org/t/replace-string-by-schema-inside-array/57199/2 "2022-01-25T11:31:27Z")

</div>

![chrome_227WfLQMLA](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/db6b0a03c7219ca91c3796bb93ccad02d6d42e26.gif)

function code...

```auto
const mobiles = [
    {
        oldWord: 'Samsung',
        newWord: 'Galaxy Note 9'
    },
    {
        oldWord: 'Google',
        newWord: 'Pixel 3'
    },
    {
        oldWord: 'Apple',
        newWord: 'iPhone X'
    }
];

let result = msg.payload;
mobiles.forEach(e => {
    const searchRegExp = new RegExp(e.oldWord, 'gi'); 
    result = result.replace(searchRegExp, e.newWord);
})

msg.payload = result;

return msg;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 February 2022 11:31 UTC](https://discourse.nodered.org/t/replace-string-by-schema-inside-array/57199/3 "2022-02-08T11:31:53Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
