I need to update the initial values of myObject with new names:
let myObject =
[ { name: 'X0', values: 'FALSE,TRUE' }
, { name: 'X1', values: 'NORMAL,LOW,HIGH' }
, { name: 'X2', values: 'HIGH,NORMAL,LOW' }
, { name: 'X3', values: 'FALSE,TRUE' }
]
I have two arrays: arr1
contains all unique values from myObject
, and arr2
has the corresponding renamed values that should replace the original ones in myObject
. They are already in the correct order:
For example, FALSE
should be replaced with JDIFS
, LOW
with T9SQK
, and so on.
let arr1 = ['FALSE', 'TRUE', 'NORMAL', 'LOW', 'HIGH' ]
let arr2 = ['JDIFS', 'CZ899', 'YVI0T', 'T9WQK', '0XCH7' ]
Expected Output :
let expected =
[ { name: 'X0', values: 'JDIFS,CZ899' }
, { name: 'X1', values: 'YVIOT,TW9WQK,0XCH7' }
, { name: 'X2', values: '0XCH7,YVIOT,T9WQK' }
, { name: 'X3', values: 'JDIFS,CZ899' }
]
I want to iterate over the object like this:
let second_column = myObject["columns"][1]
for (let i=0; i< myObject.length; i++) {
let tran = myObject[i][second_column].split(',')
// then perform the replacement using the two arrays, or by using the replace method
}