0

I have an array of objects as follows.

Array[0-2]
   0: Object
           Name: 'Rick'
           City: 'Sunnyvale'
           FactorJSONMap: "{"Id":"234","Country":"USA"}"
   1: Object
           Name: 'Diana'
           City: 'Santa Clara'
           FactorJSONMap: "{"Id":"124","Country":"USA"}"
   2: Object
           Name: 'Thiago'
           City: 'New Jeresy'
           FactorJSONMap: "{"Id":"673","Country":"USA"}"

I am able to loop through the elements 'Name' and 'City' properly. However there is an element 'FactorJSONMap'. this is coming from database. I am not able to loop through this generally.

Can anyone please let me know if there is a way to convert above format of data into the below format.

Please note. The FactorJSONMap is dynamic and there can be different elements. here it has 'Id' and 'City' currently. It can have 'Id', 'City' and 'Sex'.

Is there a way i can pull above data and convert into this simple array of objects form.

Array[0-2]
   0: Object
           Name: 'Rick'
           City: 'Sunnyvale'
           Id: '234'
           Country:'USA'
   1: Object
           Name: 'Diana'
           City: 'Santa Clara'
           Id: '124'
           Country:'USA'
   2: Object
           Name: 'Thiago'
           City: 'New Jeresy'
           Id: '673'
           Country:'USA'
Rihana
  • 403
  • 2
  • 7
  • 14
  • 1
    Have you tried parsing the JSON? `JSON.parse(json)`. – Andrew Li Jul 14 '17 at 20:46
  • @AndrewLi- how do i parse when its already inside the array of objects ? – Rihana Jul 14 '17 at 20:51
  • 1
    `JSON.parse(array[0].FactorJSONMap)` or when you're iterating over it, for example `array.forEach(obj => { const map = JSON.parse(obj.FactorJSONMap) })`? – Andrew Li Jul 14 '17 at 20:52
  • but this will only be for FactorJSONMap.? how can i add this to the array of objects and then iterate all togetheR ? – Rihana Jul 14 '17 at 20:58
  • 1
    I would think you need to flatten the object. Check this out for reference https://stackoverflow.com/questions/8750362/using-variables-with-nested-javascript-object – johnny_mac Jul 14 '17 at 21:03

1 Answers1

1

You can simply use the parse method of the JSON object to get the data within the FactorJSONMap into object format, then assign it to the parent object within the array. Once you've iterated through all Objects in the array, the returned Object Array will have all the nested properties listed within the first level.

https://jsfiddle.net/4h72h6b8/3/

function traverseData(data) {
  for(let data_obj of data) {
  Object.assign(data_obj, JSON.parse(data_obj.FactorJSONMap));
  delete data_obj.FactorJSONMap;
  }
  return data;
};

In the example you will see the result in the console log.

zfrisch
  • 8,474
  • 1
  • 22
  • 34
  • Can you also share knowledge about asign and delete function. i have never come across this in javascript. But this works well. thanks. – Rihana Jul 14 '17 at 21:32
  • 1
    When you use Object.assign(target, object) - Properties in the target object will be overwritten by properties in the sources if they have the same key. Later sources' properties will similarly overwrite earlier ones. delete is simply a keyword that removes a property from an Object. – zfrisch Jul 14 '17 at 21:43