skip to Main Content

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


  1. 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.

    Object.entries(tracelogMaskData).forEach(([key, value], index) => {
      tracelogMaskData[key] = 2 ** index
    });
    

    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.

    Object.keys(tracelogMaskData).forEach(key, index) => {
      tracelogMaskData[key] = 2 ** index
    });
    
    Login or Signup to reply.
  2. 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 corresponding masks index (and value) then extract the field name by removing the mask value suffix.

    const checkedData = {0: 'infoclient', 1: 'warnmanager', 2: 'errorserver', 3: 'warnpublic', 4: 'errornetwork'};
    const masks = {value: ['info', 'warn', 'error', 'threadentry', 'tracefunc']};
    const tracelogMaskData = { client: 8, manager: 15, server: 7, public: 18, network: 6 };
    
    // this has terrible time complexity but without a delimiter in the keys,
    // it's the best you can do
    Object.values(checkedData).forEach((key) => {
      // find the mask index
      const exp = masks.value.findIndex((mask) => key.startsWith(mask));
      
      if (exp !== -1) {
        const field = key.slice(masks.value[exp].length);
        if (Object.hasOwn(tracelogMaskData, field)) {
          tracelogMaskData[field] = 2 ** exp;
        }
      }
    });
    
    console.log('tracelogMaskData:', tracelogMaskData);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search