0

So I am trying to populate a single object. My data looks like this right now:

[ { 'id1': 
     { name: 'K Mmm',
       time: '1583308800/1583395199',
       ref: 'JK92fYxGmCofsWa1DMDMKXSo/JK92fYxGmCofsWa1DMDMKXSo' } },
  { 'id2': 
     { name: 'Bb Bb',
       time: '1583125200/1583222399',
       ref: 'JK92fYxGmCofsWa1DMDMKXSo/JK92fYxGmCofsWa1DMDMKXSo' } } ]

And I would like it to look like this:

{'id1': 
     { name: 'K Mmm',
       time: '1583308800/1583395199',
       ref: 'JK92fYxGmCofsWa1DMDMKXSo/JK92fYxGmCofsWa1DMDMKXSo' } ,
 'id2': 
     { name: 'Bb Bb',
       time: '1583125200/1583222399',
       ref: 'JK92fYxGmCofsWa1DMDMKXSo/JK92fYxGmCofsWa1DMDMKXSo' } ,
... }

The problem is that I am getting a object in a for loop, is there any way to populate an object from scratch? Right not I am pushing to Array with .push(). Like push but to a object instead to an array?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Spec
  • 35
  • 1
  • 2
  • 5
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan Mar 02 '20 at 13:57
  • if you were to store the array in a variable `dataArr` you could create a object `dataObj` by using the following code; `var dataObj = {}; for(var e in dataArr) dataObj[Object.keys(dataArr[e])] = dataArr[e][Object.keys(dataArr[e])];` it is kind of cheating but it does the job – Steinar Mar 02 '20 at 14:21
  • For some reason the Solution there doesnt work for me, I already tried this answer.. I am using es7 – Spec Mar 02 '20 at 16:12

0 Answers0