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'
5
  • 1
    Have you tried parsing the JSON? JSON.parse(json). Commented Jul 14, 2017 at 20:46
  • @AndrewLi- how do i parse when its already inside the array of objects ? Commented Jul 14, 2017 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) })? Commented Jul 14, 2017 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 ? Commented Jul 14, 2017 at 20:58
  • 1
    I would think you need to flatten the object. Check this out for reference stackoverflow.com/questions/8750362/… Commented Jul 14, 2017 at 21:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you also share knowledge about asign and delete function. i have never come across this in javascript. But this works well. thanks.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.