iterate a complex JSON structure in angular 5
how to iterate a JSON in angular 5? Have been searching a lot a got a pipe concept but it does not work with complex json something like below . I need to create expandable table using below type of data, I am stuck in reading this json.
data = [{
"Items" : [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
, .... ]
angular typescript
add a comment |
how to iterate a JSON in angular 5? Have been searching a lot a got a pipe concept but it does not work with complex json something like below . I need to create expandable table using below type of data, I am stuck in reading this json.
data = [{
"Items" : [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
, .... ]
angular typescript
Possible duplicate of How do I loop through or enumerate a JavaScript object?
– sloth
Mar 15 '18 at 10:41
no... its in angular template not in javascript.i suppose both are diffrent
– PPB
Mar 15 '18 at 10:45
Possible duplicate of access key and value of object using *ngFor
– sudheer KB
Mar 15 '18 at 11:52
no.. using pipe only simple json can be accessed, json object containing json arrays return [object obect] for 'Attributes' key
– PPB
Mar 15 '18 at 12:50
add a comment |
how to iterate a JSON in angular 5? Have been searching a lot a got a pipe concept but it does not work with complex json something like below . I need to create expandable table using below type of data, I am stuck in reading this json.
data = [{
"Items" : [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
, .... ]
angular typescript
how to iterate a JSON in angular 5? Have been searching a lot a got a pipe concept but it does not work with complex json something like below . I need to create expandable table using below type of data, I am stuck in reading this json.
data = [{
"Items" : [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
, .... ]
angular typescript
angular typescript
edited Mar 15 '18 at 12:48
PPB
asked Mar 15 '18 at 10:39
PPBPPB
972414
972414
Possible duplicate of How do I loop through or enumerate a JavaScript object?
– sloth
Mar 15 '18 at 10:41
no... its in angular template not in javascript.i suppose both are diffrent
– PPB
Mar 15 '18 at 10:45
Possible duplicate of access key and value of object using *ngFor
– sudheer KB
Mar 15 '18 at 11:52
no.. using pipe only simple json can be accessed, json object containing json arrays return [object obect] for 'Attributes' key
– PPB
Mar 15 '18 at 12:50
add a comment |
Possible duplicate of How do I loop through or enumerate a JavaScript object?
– sloth
Mar 15 '18 at 10:41
no... its in angular template not in javascript.i suppose both are diffrent
– PPB
Mar 15 '18 at 10:45
Possible duplicate of access key and value of object using *ngFor
– sudheer KB
Mar 15 '18 at 11:52
no.. using pipe only simple json can be accessed, json object containing json arrays return [object obect] for 'Attributes' key
– PPB
Mar 15 '18 at 12:50
Possible duplicate of How do I loop through or enumerate a JavaScript object?
– sloth
Mar 15 '18 at 10:41
Possible duplicate of How do I loop through or enumerate a JavaScript object?
– sloth
Mar 15 '18 at 10:41
no... its in angular template not in javascript.i suppose both are diffrent
– PPB
Mar 15 '18 at 10:45
no... its in angular template not in javascript.i suppose both are diffrent
– PPB
Mar 15 '18 at 10:45
Possible duplicate of access key and value of object using *ngFor
– sudheer KB
Mar 15 '18 at 11:52
Possible duplicate of access key and value of object using *ngFor
– sudheer KB
Mar 15 '18 at 11:52
no.. using pipe only simple json can be accessed, json object containing json arrays return [object obect] for 'Attributes' key
– PPB
Mar 15 '18 at 12:50
no.. using pipe only simple json can be accessed, json object containing json arrays return [object obect] for 'Attributes' key
– PPB
Mar 15 '18 at 12:50
add a comment |
1 Answer
1
active
oldest
votes
You can use Object.keys
to get all properties of an object. If you want to use it in an angular template, you have to create a method in your component that wraps it.
Here I hacked together a simple example, using a template to created nested tables.
You'll get the idea:
//our root app component
import Component, NgModule, VERSION from '@angular/core'
import BrowserModule from '@angular/platform-browser'
@Component(
selector: 'my-app',
template: `
<ng-template #table let-obj="obj">
<table style="border: 1px solid black">
<thead>
<tr>
<td *ngFor="let key of getKeys(obj)">
key
</td>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor="let value of getValues(obj)">
<div *ngIf="isValue(value)">
value
</div>
<div *ngIf="!isValue(value)">
<ng-container *ngTemplateOutlet="table;context:ctx(value)"></ng-container>
</div>
</td>
</tr>
</tbody>
</table>
</ng-template>
<div *ngFor="let d of data">
<ng-container *ngTemplateOutlet="table;context:ctx(d)"></ng-container>
</div>
`,
)
export class App
data:any;
constructor()
this.data = [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
];
ctx(obj)
return obj ;
getKeys(obj)
return Object.keys(obj);
getValues(obj)
return Object.values(obj);
isValue(obj)
return (obj !== Object(obj));
@NgModule(
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
)
export class AppModule
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49297271%2fiterate-a-complex-json-structure-in-angular-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use Object.keys
to get all properties of an object. If you want to use it in an angular template, you have to create a method in your component that wraps it.
Here I hacked together a simple example, using a template to created nested tables.
You'll get the idea:
//our root app component
import Component, NgModule, VERSION from '@angular/core'
import BrowserModule from '@angular/platform-browser'
@Component(
selector: 'my-app',
template: `
<ng-template #table let-obj="obj">
<table style="border: 1px solid black">
<thead>
<tr>
<td *ngFor="let key of getKeys(obj)">
key
</td>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor="let value of getValues(obj)">
<div *ngIf="isValue(value)">
value
</div>
<div *ngIf="!isValue(value)">
<ng-container *ngTemplateOutlet="table;context:ctx(value)"></ng-container>
</div>
</td>
</tr>
</tbody>
</table>
</ng-template>
<div *ngFor="let d of data">
<ng-container *ngTemplateOutlet="table;context:ctx(d)"></ng-container>
</div>
`,
)
export class App
data:any;
constructor()
this.data = [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
];
ctx(obj)
return obj ;
getKeys(obj)
return Object.keys(obj);
getValues(obj)
return Object.values(obj);
isValue(obj)
return (obj !== Object(obj));
@NgModule(
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
)
export class AppModule
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
add a comment |
You can use Object.keys
to get all properties of an object. If you want to use it in an angular template, you have to create a method in your component that wraps it.
Here I hacked together a simple example, using a template to created nested tables.
You'll get the idea:
//our root app component
import Component, NgModule, VERSION from '@angular/core'
import BrowserModule from '@angular/platform-browser'
@Component(
selector: 'my-app',
template: `
<ng-template #table let-obj="obj">
<table style="border: 1px solid black">
<thead>
<tr>
<td *ngFor="let key of getKeys(obj)">
key
</td>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor="let value of getValues(obj)">
<div *ngIf="isValue(value)">
value
</div>
<div *ngIf="!isValue(value)">
<ng-container *ngTemplateOutlet="table;context:ctx(value)"></ng-container>
</div>
</td>
</tr>
</tbody>
</table>
</ng-template>
<div *ngFor="let d of data">
<ng-container *ngTemplateOutlet="table;context:ctx(d)"></ng-container>
</div>
`,
)
export class App
data:any;
constructor()
this.data = [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
];
ctx(obj)
return obj ;
getKeys(obj)
return Object.keys(obj);
getValues(obj)
return Object.values(obj);
isValue(obj)
return (obj !== Object(obj));
@NgModule(
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
)
export class AppModule
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
add a comment |
You can use Object.keys
to get all properties of an object. If you want to use it in an angular template, you have to create a method in your component that wraps it.
Here I hacked together a simple example, using a template to created nested tables.
You'll get the idea:
//our root app component
import Component, NgModule, VERSION from '@angular/core'
import BrowserModule from '@angular/platform-browser'
@Component(
selector: 'my-app',
template: `
<ng-template #table let-obj="obj">
<table style="border: 1px solid black">
<thead>
<tr>
<td *ngFor="let key of getKeys(obj)">
key
</td>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor="let value of getValues(obj)">
<div *ngIf="isValue(value)">
value
</div>
<div *ngIf="!isValue(value)">
<ng-container *ngTemplateOutlet="table;context:ctx(value)"></ng-container>
</div>
</td>
</tr>
</tbody>
</table>
</ng-template>
<div *ngFor="let d of data">
<ng-container *ngTemplateOutlet="table;context:ctx(d)"></ng-container>
</div>
`,
)
export class App
data:any;
constructor()
this.data = [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
];
ctx(obj)
return obj ;
getKeys(obj)
return Object.keys(obj);
getValues(obj)
return Object.values(obj);
isValue(obj)
return (obj !== Object(obj));
@NgModule(
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
)
export class AppModule
You can use Object.keys
to get all properties of an object. If you want to use it in an angular template, you have to create a method in your component that wraps it.
Here I hacked together a simple example, using a template to created nested tables.
You'll get the idea:
//our root app component
import Component, NgModule, VERSION from '@angular/core'
import BrowserModule from '@angular/platform-browser'
@Component(
selector: 'my-app',
template: `
<ng-template #table let-obj="obj">
<table style="border: 1px solid black">
<thead>
<tr>
<td *ngFor="let key of getKeys(obj)">
key
</td>
</tr>
</thead>
<tbody>
<tr>
<td *ngFor="let value of getValues(obj)">
<div *ngIf="isValue(value)">
value
</div>
<div *ngIf="!isValue(value)">
<ng-container *ngTemplateOutlet="table;context:ctx(value)"></ng-container>
</div>
</td>
</tr>
</tbody>
</table>
</ng-template>
<div *ngFor="let d of data">
<ng-container *ngTemplateOutlet="table;context:ctx(d)"></ng-container>
</div>
`,
)
export class App
data:any;
constructor()
this.data = [
"Key" : "9009",
"type" : "fsdf",
"name" : "sdfsdfn",
"spec" : "dsfsdfdsf",
"Attributes" :
"category" : "Mobile",
"orderDate" : "2019-03-07 14:40:49.2",
"startDate" : "2019-03-07 14:40:49.2",
"status" : "CREATED"
,
"characteristics" : [ ],
"relatedItems" :
"contains" : [ "1", "2", "3"]
,
"sellable" : false
];
ctx(obj)
return obj ;
getKeys(obj)
return Object.keys(obj);
getValues(obj)
return Object.values(obj);
isValue(obj)
return (obj !== Object(obj));
@NgModule(
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
)
export class AppModule
edited Nov 13 '18 at 9:06
answered Mar 15 '18 at 11:47
slothsloth
75k14129172
75k14129172
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
add a comment |
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
Perfect solution! :)
– PPB
Mar 15 '18 at 12:07
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f49297271%2fiterate-a-complex-json-structure-in-angular-5%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of How do I loop through or enumerate a JavaScript object?
– sloth
Mar 15 '18 at 10:41
no... its in angular template not in javascript.i suppose both are diffrent
– PPB
Mar 15 '18 at 10:45
Possible duplicate of access key and value of object using *ngFor
– sudheer KB
Mar 15 '18 at 11:52
no.. using pipe only simple json can be accessed, json object containing json arrays return [object obect] for 'Attributes' key
– PPB
Mar 15 '18 at 12:50