0

I have array of objects in which each object having one object as below.

[{
    "-KzbaPS3vuZTG2LbuB3j": {
        "age": "24",
        "branchname": "columbia",
        "city": "hyd",
        "date": 1511418820987,
        "gender": "M",
        "id": "rcjcx8",
        "mobile": "9898989898",
        "name": "Patient1",
        "op": 100
    }
}, {
    "-KzTh7RTtTWviL4HkHv0": {
        "age": "21",
        "branchname": "kims",
        "city": "vizag",
        "date": 1511334303070,
        "gender": "F",
        "id": "45krxb",
        "mobile": "9898989898",
        "name": "aruna",
        "op": 100
    },
    "-KzTiCIheMPJIpJhPXQJ": {
        "age": "22",
        "branchname": "kims",
        "city": "hyderabad",
        "date": 1511420593865,
        "gender": "F",
        "id": "c7iqws",
        "mobile": "9878787878",
        "name": "vineesha",
        "op": 100
    },
    "-KzTnzLnRsdFWg-m5b9U": {
        "age": "26",
        "branchname": "kims",
        "city": "bheemavaram",
        "date": 1511420593865,
        "gender": "F",
        "id": "ujtgz",
        "mobile": "9876787898",
        "name": "Madhavi",
        "op": 100
    }
}]

The object is not known, then how can I have the all the objects of objects into an array?

0

3 Answers 3

1

You can use

array.forEach(obj => {
    let key = Object.keys(obj)[0];
    let yourValue = obj[key]; 
    // yourValue = { "age": "21", "id": "rcjcx8" ...} in your case for the first item
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you are fine with Lodash, you can do the following:

// assuming the above data is in variable "data"

let structuredData = _.flatten(data).reduce((a,c) => _.assign(c), {});

Comments

0

Iterate and get the keys and use the key to get the value. Working version

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular 5';
  jsonDataKeys : any=[];

  jsonData : any =[{
    "-KzbaPS3vuZTG2LbuB3j": {
        "age": "24",
        "branchname": "columbia",
        "city": "hyd",
        "date": 1511418820987,
        "gender": "M",
        "id": "rcjcx8",
        "mobile": "9898989898",
        "name": "Patient1",
        "op": 100
    }
}, {
    "-KzTh7RTtTWviL4HkHv0": {
        "age": "21",
        "branchname": "kims",
        "city": "vizag",
        "date": 1511334303070,
        "gender": "F",
        "id": "45krxb",
        "mobile": "9898989898",
        "name": "aruna",
        "op": 100
    },
    "-KzTiCIheMPJIpJhPXQJ": {
        "age": "22",
        "branchname": "kims",
        "city": "hyderabad",
        "date": 1511420593865,
        "gender": "F",
        "id": "c7iqws",
        "mobile": "9878787878",
        "name": "vineesha",
        "op": 100
    },
    "-KzTnzLnRsdFWg-m5b9U": {
        "age": "26",
        "branchname": "kims",
        "city": "bheemavaram",
        "date": 1511420593865,
        "gender": "F",
        "id": "ujtgz",
        "mobile": "9876787898",
        "name": "Madhavi",
        "op": 100
    }
}];

constructor(){
  for(let arr of this.jsonData){
       Object.keys(arr) && Object.keys(arr).map((key)=>{
          console.log("push")
          this.jsonDataKeys.push(key);
        });
    }
}

}

HTML file

{{jsonDataKeys}}

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>
<!--<div>{{jsonData | json}}</div>-->

 <div *ngFor="let arr of jsonData">
  <div *ngFor="let item of jsonDataKeys">
    item : {{item}}
    {{arr[item] | json}}
    <br>
    <br>
  </div>
 </div>

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.