skip to Main Content

I have object key value in below format.

{
    "Code1": {
        "char10": "ch1",
        "number1": "1",
        "text1": "txt1"
    },
    "Code2": {
        "char2": "ch2",
        "num2": "2"
    },
    "Code3": {
        "text": "txt4"
    }
}

Would like to convert to this format :

{
  "Code1": [
    {
      "char10": "ch1",
      "number1": "1",
      "text1": "txt1"
    }
  ],
  "Code2": [
    {
      "char2": "ch2",
      "num2": "2"
    }
  ],
  "Code3": [
    {
      "text": "txt4"
    }
  ]
}

Managed to achieve to get somewhat similar response but not exact output which I am looking for.

Tried the below snippet but it returns diff format than expected.

Object.entries(payload).map((e) => ( { [e[0]]: e[1] } ))

Response with above snippet :

[
    {
        "Code1": {
            "char10": "ch1",
            "number1": "1",
            "text1": "txt1"
        }
    },
    {
        "Code2": {
            "char2": "ch2",
            "num2": "2"
        }
    },
    {
        "Code3": {
            "text": "txt4"
        }
    }
]

3

Answers


  1. You could get all entries and map with wrapped values for a new object.

    const
        data = { Code1: { char10: "ch1", number1: "1", text1: "txt1" }, Code2: { char2: "ch2", num2: "2" }, Code3: { text: "txt4" } },
        result = Object.fromEntries(Object
            .entries(data)
            .map(([k, v]) => [k, [v]])
        );
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
  2. You can solve this problem by using Object.entries() with reduce() to transform each entry into an array containing an object.

    Here’s how you can do it :

    const payload = {
        "Code1": {
            "char10": "ch1",
            "number1": "1",
            "text1": "txt1"
        },
        "Code2": {
            "char2": "ch2",
            "num2": "2"
        },
        "Code3": {
            "text": "txt4"
        }
    };
    
    const transformed = Object.entries(payload).reduce((acc, [key, value]) => {
        acc[key] = [value];
        return acc;
    }, {});
    
    console.log(transformed);
    
    Login or Signup to reply.
  3. You will need to reduce the objects’s entries and for each key value pair, assign the key to the result object and supply it with the value wrapped inside of an array.

    I wrote a reusable function below, that takes an optional valueMapper which can transform the value upon assignment to the resulting object.

    const data = {
      Code1: { char10: 'ch1', number1: '1', text1: 'txt1' },
      Code2: { char2:  'ch2', num2: '2' },
      Code3: { text:   'txt4' }
    }
    
    const result = reduceEntries(data, (v) => [v]); // Wrap the value in an array
    
    console.log(result);
    
    function reduceEntries(obj, valueMapper) {
      return Object.entries(data).reduce((result, [key, value]) =>
        Object.assign(result, {
          [key]: valueMapper ? valueMapper(value, key) : value
        }) ,{});
    }
    .as-console-wrapper { top: 0; max-height: 100% !important; }

    Of course, it can also be written using Array.prototype.map and Object.fromEntries; instead of Array.prototype.reduce and Object.assign. Either solution will work.

    function reduceEntries(obj, valueMapper) {
      return Object.fromEntries(Object.entries(data).map(([key, value]) =>
        [key, valueMapper ? valueMapper(value, key) : value]));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search