3

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"
        }
    }
]
4
  • Array.map() will always return an array Commented Oct 24, 2024 at 18:28
  • 1
    I have updated the sample correctly. I am getting key value in object format and have to convert it to desired format for sending it as payload in one of the REST api. Commented Oct 24, 2024 at 18:29
  • You have an answer. Use fromEntries to take the mapped output of entries and create an object from it. You were off with your map call, that's all. Commented Oct 24, 2024 at 18:33
  • 1
    for (let key in payload) payload[key] = [payload[key]]; is a basic way. Or assign the props to a new object. Commented Oct 24, 2024 at 18:48

3 Answers 3

4

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; }

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

Comments

0

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);

Comments

0

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]));
}

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.