I have an array written in this way:
[
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'K' },
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'L' },
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'K' },
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'L' }
]
And I want to obtain an array writtenin this way:
[
{ L: 'TestMetaCoin', K: 'Contract: MetaCoin' },
{ L: ['TestMetaCoin','Contract: MetaCoin'] },
{ L: 'TestMetaCoin', K: 'Contract: MetaCoin' },
{ L: ['TestMetaCoin','Contract: MetaCoin'] }
]
I tryed to use the nodejs function restore()
but the result is:
[
{ L: 'TestMetaCoin', K: 'Contract: MetaCoin' },
{ L: 'Contract: MetaCoin' },
{ L: 'TestMetaCoin', K: 'Contract: MetaCoin' },
{ L: 'Contract: MetaCoin' }
]
It completely overwrites the value in position 2 and 4, probably because it tries to create an existing key and therefore not being able to create it, accesses the old one by overwriting the value exists instead of creating an array with 2 values.
How can I get around this problem?
This is the code that I use:
var fristArray= [
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'K' },
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'L' },
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'K' },
{ TestMetaCoin: 'L', 'Contract: MetaCoin': 'L' }
]
var swapped=[]
for(var i=0;i<fristArray.length;i++) {
swapped.push(Object.fromEntries(Object.entries(fristArray[i]).map(([k, v]) => [v, k])))
}
console.log(swapped)