2017-11-23 9 views
0

私は、各オブジェクトが以下のような1つのオブジェクトを持つオブジェクトの配列を持っています。オブジェクトのキーがわからないときに配列のオブジェクトのオブジェクトを取得するには?

[{ 
    "-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 
    } 
}] 

オブジェクトが不明な場合は、どのようにオブジェクトのすべてのオブジェクトを配列にできますか?

答えて

0

あなたはLodashと罰金している場合は、次の操作を行うことができます。

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

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

あなたは

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

反復を使用して、キーを取得し、値を取得するためにキーを使用することができます。 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ファイル

{{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> 
関連する問題