checkedData has value
{ 0: 'infoclient', 1: 'warnmanager', 2: 'errorserver', 3: 'warnpublic', 4: 'errornetwork'}
masks has value
['info', 'warn', 'error', 'threadentry', 'tracefunc']
tracelogMaskData has values
{ client: 8, manager: 15, server: 7, public: 18,network: 6 }
In the below code, after I find the index within for loop, I want to do some manipulations using the index value. I want to change the values inside tracelogMaskData with the manipulated index value. Before that I want to do some mapping between checkedData and tracelogData.
For eg: checkedData has ‘infoclient’ which contains one of the mask values. So the index is 0. Lets say the manipulated value is "2 to the power 0".
Now I want tracelogMaskData to be
{ client: 1, manager: 2, server: 4, public: 2,network: 4 }
Here’s the code.
const indexes = Object.values(checkedData).forEach((c) => {
const index = masks.value.findIndex((m) => c.startsWith(m));
console.log(index); // do something with the index here...
Object.values(tracelogMaskData).forEach((t) => {
console.log('c & t values outside if>>>>>>>>>>>>>', c, t);
});
});
I’m able to fetch c and t values here. I’m trying to fetch the keys to the relevant t values. Also, How to change the values inside t. Also, I want to know if this code can be simplified further.
2
Answers
Instead of using Object.values, you can use Object.entries to get the keys and values. Also to change the value of t directly update the object key.
Also, if you do not want to use value of the object for adding a check, then you can simply use Object.keys and update the value.
Without a delimiter between the field names and mask values in
checkedData
, you’re limited to a pretty nasty O(n^2) search algorithm.Iterate the values in
checkedData
, find the correspondingmasks
index (and value) then extract the field name by removing the mask value suffix.