If the strings are too long or if the replacements can have the substring "xxx"
in them, then you'd be better off using a regex with the global modifier and use the callback of replace
to select an item from the array using an index:
items.forEach(item => {
let index = 0;
item.str = item.str.replace(/xxx/g, m => item.list[index++]);
});
Otherwise, your solution is just fine.
Notes:
1- You may want to check if index
goes beyond item.list.length
in case there are more xxx
than there are items in item.list
. A safe solution would be:
item.str = item.str.replace(/xxx/g, m => item.list[index++] || m);
Which replaces by m
if item.list[index++]
is undefined
.
2- If you don't care about mutating item.list
, then the whole solution could be a lot shorter by using shift
instead of the index
:
items.forEach(item => item.str.replace(/xxx/g, m => item.list.shift() || m));
Example:
var items = [{
str: 'This is xxx an he is xxx years old ',
list: ['Frank', '14']
},
{
str: 'xxx and xxx are xxx ',
list: ['George', 'John', "studying"]
}
]
items.forEach(item => {
let index = 0;
item.str = item.str.replace(/xxx/g, m => item.list[index++]);
})
console.log(items);