I have a HTML form with three input fields, and I need to make some replacements of their values.
So I wrote the following code (HTML part is omitted):
var fields = [];
fields[0] = 'aaa bbb ccc';
fields[1] = 'aaa bbb ccc ddd';
fields[2] = 'eee';
const array = [
{
'aaa': 'AAA',
'bbb': 'BBB',
'ccc': 'CCC'
},
{
'ddd': 'DDD'
},
{
'eee': 'EEE'
}
];
for (var i in array) {
for (var k in array[i]) {
fields[i] = fields[i].replace(RegExp(k, 'g'), array[i][k]);
// I can't use replaceAll, because I need to support old browsers
}
};
As you can see, in the 2nd field I have to make the same replacements than in the 1st one (aaa bbb ccc), plus another one (ddd).
How can I copy (extend?) the content of the first object into the second one?
In other words, the content of the second object should become:
{
'aaa': 'AAA',
'bbb': 'BBB',
'ccc': 'CCC',
'ddd': 'DDD'
}
I'm new to JS objects, so my terminology could be inaccurate. But I hope my problem is clear.