skip to Main Content
{
    "tin": "TIN is required",
    "bic": "BIC is required",
}

I need to put all the property values of an object into an array.

{
    "tin": ["TIN is required"],
    "bic": ["BIC is required"],
}

How to do it?

2

Answers


  1. Object.entries() gives you an array of key value pairs

    const obj = {
        "tin": "TIN is required",
        "bic": "BIC is required",
    }
    
    console.log(Object.entries(obj))

    Update due to comment:

    const obj = {
        "tin": "TIN is required",
        "bic": "BIC is required",
    }
    
    for (const [k,v] of Object.entries(obj)) {
      obj[k] = [v];
    }
    
    console.log(obj);
    Login or Signup to reply.
  2. You can simply use Object.keys(yourobject):

    console.log(Object.keys({
        "tin": "TIN is required",
        "bic": "BIC is required",
    }));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search