1

Following is my object which I want to convert to an array but seem like I am missing something here. Please let me know what I am doing wrong here.

Code -

var langObj = {
    "CON1010": {
    "countryName": "Poland",
    "countryCode": "pl",
    "localLanguages": [
        {
            "language": "English",
            "languageCode": "en"
        },
        {
            "language": "Polish",
            "languageCode": "en"
        }
      ]
    },
    "CON1011": {
        "countryName": "UK",
        "countryCode": "uk",
        "localLanguages": [
            {
                "language": "English",
                "languageCode": "en"
            }
        ]
    }
};


var res = Object.entries(langObj).map(([value, label]) => ({value, label}));
console.log(res);

Expected Output -

[
    {
        "CON1010": {
            "countryName": "Poland",
            "countryCode": "pl",
            "localLanguages": [
                {
                    "language": "English",
                    "languageCode": "en"
                },
                {
                    "language": "Polish",
                    "languageCode": "en"
                }
            ]
        }
    },
    {
        "CON1011": {
            "countryName": "UK",
            "countryCode": "uk",
            "localLanguages": [
                {
                    "language": "English",
                    "languageCode": "en"
                }
            ]
        }
    }
]
0

3 Answers 3

5

You need square brackets to get dynamically evaluated field name:

var res = Object.entries(langObj).map(([value, label]) => ({[value]:label}));

var langObj = {
    "CON1010": {
      "countryName": "Poland",
      "countryCode": "pl",
      "localLanguages": [
        {
          "language": "English",
          "languageCode": "en"
        },
        {
          "language": "Polish",
          "languageCode": "en"
        }
      ]
    },
    "CON1011": {
      "countryName": "UK",
      "countryCode": "uk",
      "localLanguages": [
        {
          "language": "English",
          "languageCode": "en"
        }
      ]
    }
};


var res = Object.entries(langObj).map(([value, label]) => ({[value]:label}));
console.log(res);

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

Comments

2

Just a bit off, it should be

var res = Object.entries(langObj).map(([key, value]) => ({ [key]: value }));

Comments

1

You are setting value and label in your object. You want to use the "value" as the key. So you want to return { [value]: label }

var langObj = {
  "CON1010": {
    "countryName": "Poland",
    "countryCode": "pl",
    "localLanguages": [{
        "language": "English",
        "languageCode": "en"
      },
      {
        "language": "Polish",
        "languageCode": "en"
      }
    ]
  },
  "CON1011": {
    "countryName": "UK",
    "countryCode": "uk",
    "localLanguages": [{
      "language": "English",
      "languageCode": "en"
    }]
  }
};


var res = Object.entries(langObj)
  .map(([value, label]) =>
    ({ [value]: label })
  );
console.log(res);

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.