2016-09-07 12 views
2

Ich arbeite an einem Projekt mit Polymer 1.0 und ich möchte dom-repeat verwenden, um Daten von Firebase 3.0 aufzulisten.Javascript: Konvertieren von Objekten von Objekten in Array von Objekten

In Firebase habe ich ein Objekt von Objekten wie folgt aus:

var objectofobjects = { 
    "-KR1cJhKzg9uPKAplLKd" : { 
     "author" : "John J", 
     "body" : "vfdvd", 
     "time" : "September 6th 2016, 8:11", 
     "title" : "vfvfd" 
    }, 
    "-KR1cLZnewbvo45fDnEf" : { 
     "author" : "JJ", 
     "body" : "vfdvdvf", 
     "time" : "September 6th 2016, 8:11", 
     "title" : "vfvfdvfdv" 
    } 
}; 

und ich möchte es von Objekten wie dies in ein Array konvertieren:

var arrayofobjects = [ { '-KR1cJhKzg9uPKAplLKd': 
{ author: 'John J', 
    body: 'vfdvd', 
    time: 'September 6th 2016, 8:11', 
    title: 'vfvfd' }, 
'-KR1cLZnewbvo45fDnEf': 
{ author: 'JJ', 
    body: 'vfdvdvf', 
    time: 'September 6th 2016, 8:11', 
    title: 'vfvfdvfdv' } } ]; 
+2

Diese zweite Struktur ist nicht gültig JSON sein. http://jsonlint.com/ –

Antwort

1

Sie es auf diese einfache Weise tun können:

var arrObj = []; 
var obj = JSON.stringify(objectofobjects, function(key, value) { 
    arrObj.push(value); 
}) 
console.log(arrObj); 

Und die output wird dies:

[{ 
    '-KR1cJhKzg9uPKAplLKd': { 
     author: 'John J', 
     body: 'vfdvd', 
     time: 'September 6th 2016, 8:11', 
     title: 'vfvfd' 
    }, 
    '-KR1cLZnewbvo45fDnEf': { 
     author: 'JJ', 
     body: 'vfdvdvf', 
     time: 'September 6th 2016, 8:11', 
     title: 'vfvfdvfdv' 
    } 
}] 

Hinweis: Die von Ihnen erwähnte Ausgabe ist kein gültiges JSON-Array.

Ich hoffe, das sollte funktionieren.

1

noch optimieren kann, aber dies wird erhalten Du hast dein Ergebnis.

var result = []; 
for (var item in objectofobjects) { 
    if (objectofobjects.hasOwnProperty(item)) { 
    var key = item.toString(); 
    result.push({key: objectofobjects[item]}); 
    } 
} 
console.log(result); 

Das Innere Prüfung basiert auf Iterate through object properties

+0

Aber das ist nicht das Format, das das OP sagte er wollte erstellen. –

3

Ich benutze diese Mine

let arrayOfObjects = Object.keys(ObjectOfObjects).map(key => { 
    let ar = ObjectOfObjects[key] 

    // Apppend key if one exists (optional) 
    ar.key = key 

    return ar 
}) 

In diesem Fall zu konvertieren, würde die Ausgabe

[ 
    { 
    "author" : "John J", 
    "body" : "vfdvd", 
    "time" : "September 6th 2016, 8:11", 
    "title" : "vfvfd", 
    "key": "-KR1cJhKzg9uPKAplLKd" 
    }, 
    { 
    "author" : "JJ", 
    "body" : "vfdvdvf", 
    "time" : "September 6th 2016, 8:11", 
    "title" : "vfvfdvfdv", 
    "key": "KR1cLZnewbvo45fDnEf" 
    } 
] 
0
objectofobjects = [objectofobjects]; // Simplest way to do this convertation. 

JSON.stringify(objectofobjects); 
"[{"-KR1cJhKzg9uPKAplLKd":{"author":"John J","body":"vfdvd","time":"September 6th 2016, 8:11","title":"vfvfd"},"-KR1cLZnewbvo45fDnEf":{"author":"JJ","body":"vfdvdvf","time":"September 6th 2016, 8:11","title":"vfvfdvfdv"}}]" 
Verwandte Themen