javascript Transform an object into an array of objects

How can I transform an object into an array of objects?

Input:

{
  AF: 'Afghanistan',
  AX: 'Åland Islands',
  AL: 'Albania',
  DZ: 'Algeria'
}

Output:

[
    {value: 'AF', label: 'Afghanistan'},
    {value: 'AX', label: 'Åland Islands'},
    {value: 'AL', label: 'Albania'},
    {value: 'DZ', label: 'Algeria'}
]

Answer №1

Utilize the Object.keys() function to retrieve the keys of an object. This function will produce an array containing the keys as string entries. Subsequently, apply the map method to this array, and specify the desired object in the mapping function to obtain the desired array result.

let countries = {
  AF: 'Afghanistan',
  AX: 'Åland Islands',
  AL: 'Albania',
  DZ: 'Algeria'
}

let arr = Object.keys(countries).map(key => {
   return {key: key, value: countries[key]}
});
console.log(arr);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display the item exactly where the cursor is located once the transform scale is implemented

How can I accurately determine the mouse position when a transform scale, such as transform: scale(1.6);, is applied? Currently, the popup element is displaying too far down and to the right in this scenario. Check out the example here $(" ...

What might be causing my dropdown menu to be unresponsive in HTML?

In this HTML code, I opted to use images as dropdown elements instead of traditional buttons or text. /* Dropdown Button / .dropbtn { background-image: url(Images/Buttons/Resources3.png); width: 110px; height: 40px; } / The container <div> - neede ...

Issues arise when using jQuery's ajax() method with processData set to false, as it still adds form data to the URL, among

Today, I am delving into the world of jQuery to enhance a form's functionality, particularly to interact with a MySQL database. Here is the form I am working with: <form class="loginform" id="loginform" name="loginform"> <input type="ema ...

Understanding the 'this' variable in Javascript

Recently, I delved into the world of Javascript functions while reading "Javascript: The Good Parts." To test some properties, I created a script that left me puzzled by the results. Here's the code snippet: <h3>Object</h3> <di ...

Enhancing an options tag with Dojo Framework

I am experiencing difficulties updating the label of a selected option in a Dojo-created select field, despite having a form to 'rename' the selected option. Various methods have been attempted: var selectDropdown = registry.byId("stateSelect") ...

Switch between various height and width options using JavaScript

Is there a way to create a toggle that changes both the height and width of an element when it is clicked? <div class="flexbox-container" id="main" onclick="staybig()">Create Account</div> .flexbox-container { ...

How to focus on an input element in Angular 2/4

Is there a way to focus on an input element using the (click) event? I'm attempting to achieve this with the following code, but it seems like there may be something missing. (I am new to Angular) sTbState: string = 'invisible'; private ele ...

Retrieve the value of a property in a directive's isolated scope

Is there a way to access the isolated scope's property in the directive tag? Let's take a look at a simplified example: angular.module('app', []) .controller('myController', function() { var result_el = document ...

Creating a request again after a promise has been fulfilled in Angular

I'm struggling to understand a simple concept here. My objective is to retrieve data from a service, which fetches data from an endpoint, and then be able to update that stored data by refreshing the endpoint. .service('myService', function ...

JQuery scrolling animation not functioning on a specific page

I'm having trouble figuring out why my animation isn't working on just one specific page. Check it out here: As a test, I added a small gray square in the top left corner. When clicked, the script should scroll you down a bit. However, it seems ...

Retrieving JQuery Results Based on List Items

Once I have obtained the list id from the navigation, my next step is to send the id to a PHP file that will return a JSON list. Below is the jQuery syntax I am using: $("#ul_navigation li").click(function(e) { idsec = this.id; alert(idse ...

What is the best way to assign attributes to a component that is rendered by the router in Vue.js?

Seeking advice on passing data to components in VueJs. I am relatively new to VueJs and facing a challenge with two components. The issue arises when trying to display a component using the vuejs router that requires passing properties. Since the component ...

Is there a more efficient method to choose specific UV coordinates from a texture for presentation on a sprite using three.js?

I've got a texture sheet that contains various game HUD elements UV packed together in one texture. Currently, I find myself having to create a clone() of the texture for each individual part of the GUI, then create a material using this texture in o ...

Challenges regarding OAuth2 and the angular-auth2-oidc Library - Utilizing PKCE Code Flow

As a newcomer to OAuth2 and the angular-auth2-oidc library, I may make some beginner mistakes, so please bear with me. MY GOAL: I aim to have a user click on the login link on the home page, be directed to the provider's site to log in, and then retu ...

The controller method in Laravel is not being triggered by AJAX

In my blade layout file, I have some code that is included in my view multiple times using the @include.layout("blade file link") syntax. When a button is pressed, I want to call a controller function. This works fine without AJAX, but when AJAX is added, ...

In need of Javascript assistance for checks with if, or, and while conditions

Greetings, I am reaching out for assistance as I have hit a roadblock in my coding endeavors. Although such requests are not common for me, I am in need of guidance with the following code snippet: function insertVideo(link) { if (link) { if(link. ...

Combining one item from an Array Class into a new array using Typescript

I have an array class called DocumentItemSelection with the syntax: new Array<DocumentItemSelection>. My goal is to extract only the documentNumber class member and store it in another Array<string>, while keeping the same order intact. Is th ...

How can MongoDB be used to retrieve lists of birthdays for Today, Upcoming, and Past dates?

Looking for a way to retrieve Today, Upcoming and Past Birthdays using MongoDB by specifying a range of days such as the next 60 days for upcoming birthdays or the past 60 days for previous birthdays. The users' date of birth has been stored in the u ...

Seeking assistance with prototyping JS/AJAX features

For my current project, I have been utilizing PrototypeJS. However, today I ran into a problem. I am working on creating a contact list for a module, where contacts can be added via a form or removed with a link. Below is the HTML code for the removal li ...

"Integrate a URL segment into the primary URL with the help of AngularJS or JavaScript

One interesting case involves a URL path that is structured in the following way. http://localhost:12534/urlpart1/urlpart2?querystring=140 The challenge here is to replace "urlpart2" with "urlpart3" using either javascript or AngularJS. One approach is t ...