iterate a complex JSON structure in angular 5










2















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
, .... ]









share|improve this question
























  • 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
















2















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
, .... ]









share|improve this question
























  • 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














2












2








2








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
, .... ]









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 15 '18 at 12:48







PPB

















asked Mar 15 '18 at 10:39









PPBPPB

822413




822413












  • 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











  • 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













1 Answer
1






active

oldest

votes


















3














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





share|improve this answer

























  • Perfect solution! :)

    – PPB
    Mar 15 '18 at 12:07










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



);













draft saved

draft discarded


















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









3














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





share|improve this answer

























  • Perfect solution! :)

    – PPB
    Mar 15 '18 at 12:07















3














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





share|improve this answer

























  • Perfect solution! :)

    – PPB
    Mar 15 '18 at 12:07













3












3








3







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





share|improve this answer















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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 9:06

























answered Mar 15 '18 at 11:47









slothsloth

73.9k14127168




73.9k14127168












  • 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





Perfect solution! :)

– PPB
Mar 15 '18 at 12:07

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo