0

What is the best way to convert:

Dynamically I am getting an array.

For example we can consider this following array.

[
    'typeRead_1',
    'typeModify_1',
    'typeModify_2',
    'typeRead_3',
];

But I want it something like this. to:

{
    1: {
      typeRead: true,
      typeModify: true,
    },
    2: {
      typeRead: false,
      typeModify: true,
    },
    3: {
      typeRead: true,
      typeModify: false,
    }
  };
0

1 Answer 1

1

Here is one way of doing this:

const items = [
    'typeRead_1',
    'typeModify_1',
    'typeModify_2',
    'typeRead_3',
];

const ret = items.reduce((ret, el) => {
      let [prop, key] = el.split('_');
      if (!ret[key]) ret[key] = { typeRead: false, typeModify: false };
      ret[key][prop] = true;
      return ret;
    }, {})
    
console.log(ret)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.