First let me apologize for my poor english and javascript skills, I am a beginner.
Here is my problem: I would like to copy the key and values of a source object (words.wordFailed[ tmp ]) into a target object (words.wordsFailedOnly[ counter ]):
words.wordsFailedOnly[ counter ] = words.wordsFailed[ tmp ];
Once this is done I add a new key/value pair to the target:
words.wordsFailedOnly[ counter ][ 'newKey' ] = tmp;
Even though I don't assign the new key to the source object, after the first foreach loop has been executed, I noticed that the source object has also this new key "newKey".
Here is the complete code portion:
settings.languages.forEach( function( lang ) {
let counter = 0;
Object.keys( words.wordsFailed ).forEach( function ( tmp ) {
if ( words.wordsFailed[ tmp ][ lang ] > 0 ) {
counter++;
words.wordsFailedOnly[ counter ] = words.wordsFailed[ tmp ];
words.wordsFailedOnly[ counter ][ 'newKey' ] = tmp;
}
});
});
In other words, with console.log(words.wordsFailed) I notice that both objects have been assigned the "newKey" key. This is a very add behavior, in my code I just create newKey for the target, at no moment I have told to add this key to the source object. I just can't understand what is going on. How can I avoid this behavior ? I need the newKey only in the target object.
Can someone help please if you have an idea?
EDIT for additional information:
Example of what the source target contains:
{deu: 3, id: 16, eng: 3}
Example of what the target contains after it has gone through the foreach loop (this is the expected result):
{deu: 3, id: 16, eng: 3, newKey: 10}
On the second foreach loop the source also contains the "newKey" key (this is not what I expected. I don't expect the source to have the "newKey" key):
{deu: 3, id: 16, eng: 3, newKey: 10}
Expected result for target:
{deu: 3, id: 16, eng: 3, newKey: 10}
Expected result for the source:
{deu: 3, id: 16, eng: 3}