The task is to convert an Object {} to an Array [] of key-value pairs using JavaScript. Introduction: Objects, in JavaScript, is it’s most important data-type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScript’s primitive data-types(Number, String, Boolean, null, undefined and symbol). Objects are more complex and each object may contain any combination of these primitive data-types as well as reference data-types, while the array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.
We can convert an Object {} to an Array [] of key-value pairs using methods discussed below: Method 1: In this method, we will use Object.keys() and map() to achieve this.
Approach: By using Object.keys(), we are extracting keys from the Object then this key passed to map() function which maps the key and corresponding value as an array, as described in the below example. Syntax:
Object.keys(obj)
Parameter: obj: It is the object whose enumerable properties are to be returned.
map(function callback(currentValue[, index[, array]]){
// Return element for new_array
}
Parameter: callback: Function that produces an element of the new Array
Method 2: In this method, we will use Object.entries() to achieve this. Approach: We will use Object.entries() which is available in JavaScript. Object.entries() method is used to return an array consisting of enumerable property [key, value] pairs of the object which are passed as the parameter. The ordering of the properties is the same as that given by looping over the property values of the object manually.
Syntax:
Object.entries(obj)
Parameter: obj: It is the object whose enumerable own property [key, value] pairs are to be returned.
I created an interface object called language which has id and name properties.
In component ts file added a variable selectedObject which of object language and will bind it to select element using [ngModel].
And another variable languageObjects which contains list of objects which is used to display select options using [ngValue].
<h1>Bind select Element to Object</h1>
<select [(ngModel)]="selectedObject">
<option *ngFor="let lang of languageObjects" [ngValue]="lang">
{{lang.name}}
</option>
</select>
{{selectedObject | json}}
Now the selectedObject contains language object itself.
And we can display it using json pipe as shown below.
I will explain how to create an Angular 7 app with a simple “Hello World” example. This hello world example work in Angular 4,Angular 5,Angular 6,Angular7.
Let us start with Angular Hello World example to know the basics of Angular 7.
Before reading further, please go through my previous tutorial on how to setup local development environment for Angular.
We will use Angular CLI as mentioned in the above tutorial to create our Angular application.
Here are the steps to create first hello world application in Angular 7
Table of Contents
Creating First Angular 7 Hello World Example
We create a project called Hello World in Angular 7.
I recommend you to create a folder in your local disk so that all Angular examples are one place.(For instance create a folder named AngularExamples).
Open command prompt navigate to created folder; use the ng new command (Angular CLI) to create a new Angular project from scratch.
ng new hello-world-angular
It will take some time to create new project.
If you are using latest version of Angular CLI by default it uses Angular 7 (i.e., latest angular version). And there is no much difference between Angular 2 or Angular 4 or other versions it’s just an update. Please go through the following article to know more about this versioning of Angular.
If you know java or C# it is easy to understand but do not worry, we will understand each line one by one.
Here we are declaring a class named “HelloWorldComponent” which is a component.
Importing Dependencies in Component in Angular 7
First line tells the compiler that import “Component” and “OnInit” objects from @angular/core (node module installed when we created project via Angular CLI).
Component Decorators in Angular 7
After importing Component object, we have to tell the compiler that this class is a Component.
In Angular 7 (or Angular) we will use Decorators in typescript to do this. So Decorators are nothing but metadata added to our code.
For example in \hello-world-angular\src\app\app-module.ts file, Decorator tells that it is a class named “AppModule” which is a Module (NgModule).
Selector property indicates the tag name we are going to use in DOM.
<app-hello-world></app-hello-world >
(While creating component we gave name as “hello-world” Angular CLI added app as prefix).
TemplateUrl in Angular 7 Component
<app-hello-world> tag uses hello-world.component.html file as html template. So whenever we use <app-hello-world></app-hello-world> it will display the contents of file HTML located in
We have rendered static template, now we will add some data to the component.
Open “hello-world.component.ts” file in editor.
And add the name property as shown below.
name:string;
We are declaring a property or variable named “name” which is a string type. This is similar to declaration of variables in object-oriented languages. If we assign any type other than string the compiler will throw error.
In constructor we will assign name variable. Constructor will be called whenever we create a new instance of a class. Similar to C# or Java language.
Rendering Angular 7 template
Now we have to use the declared variable in our template file i.e., HTML file. We have to use two curley brackets to display the value of variable {{ }}. We will call them template tags.
To use the name variable open the template file hello-world.component.html
<p>
hello-world works!
{{name}}
</p>
As the template bind to component(hello-world component) whenever compiler encounters flower brackets(i.e., template tags) it will replace the text with bounded property (i.e., name) value i.e., ”Angular Js Wiki”.