2

I'm working on a project using Polymer 1.0 and I want to use dom-repeat to list data from Firebase 3.0.

In Firebase I have an object of objects like this:

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"
    }
};

and I want to convert it to an array of objects like this:

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' } } ];
mayijia66fly
  • 75
  • 1
  • 8

4 Answers4

6

I use this to convert mine

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

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

   return ar
})

In this case, your output would be

[
  {
    "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"
   }
 ]
gngchrs
  • 468
  • 4
  • 11
1

Can still optimize but this will get you your result.

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

The check inside is based on Iterate through object properties

Community
  • 1
  • 1
g3ctong
  • 86
  • 4
1

You can do it in this simple way:

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

And the output will be this:

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

Note: The output which you have mentioned is not a valid JSON array.

Hope this should work.

Tristan
  • 3,530
  • 3
  • 30
  • 39
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
  • 1
    Is it just me, or does this technically not answer the question? You're right about it not being a valid JSON array, but that's exactly what the question is asking for. How do you convert an object of objects, into an array of objects. This answer wraps the top level object inside an array, but still leaves the children objects wrapped inside another object. – Chase May 03 '18 at 23:14
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"}}]"
iEfimoff
  • 342
  • 4
  • 8