1

I have an array of Objects which will always be structured like so:

var initialJSON = [  
  {  
     "id":"01",
     "name":"Jane",
     "project":{  
        "title":"Alpha",
        "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
        "city":"New York"
     },
     "dateCreated":"2018-04-28 04:11:16 UTC"
   },
   {  
     "id":"02",
     "name":"Sarah",
     "project":{  
       "title":"Beta",
       "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
       "city":"London"
     },
      "dateCreated":"2018-04-28 04:11:29 UTC"
    },
    {  
      "id":"03",
      "name":"Mia",
      "project":{  
         "title":"Gamma",
         "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
         "city":"Paris"
      },
      "dateCreated":"2018-04-28 04:11:29 UTC"
   }
];

My goal is to create a new array of Objects containing just the key/values of each item's projects. Essentially removing all of the other data (including the parent key 'project') from each item.

The end result would look like so:

var formattedJSON = [
  {
    "title":"Alpha",
    "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    "city":"New York"
  },
  {
    "title":"Beta",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    "city":"London"
  },
  {
    "title":"Gamma",
    "description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit",
    "Paris"
  }
]

I am having difficulty finding a way to loop through the data and achieve the correct result. I am aware there are similar questions already posted but non seem to create a new array out of the nested key/values of an inner key.

3 Answers 3

2

You can use Array#map() for this, by assigning each element of the array to it's project property:

let initialJSON=[{"id":"01","name":"Jane","project":{"title":"Alpha","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"New York"},"dateCreated":"2018-04-28 04:11:16 UTC"},{"id":"02","name":"Sarah","project":{"title":"Beta","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"London"},"dateCreated":"2018-04-28 04:11:29 UTC"},{"id":"03","name":"Mia","project":{"title":"Gamma","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"Paris"},"dateCreated":"2018-04-28 04:11:29 UTC"}];

let result = initialJSON.map(element => element.project);

console.log(result)

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

Comments

0

You could use the map function on the initialJSON array to achieve this, in the following way:

var formattedJSON = initialJSON.map(item => {

  // For each item in the "initialJSON", map the fields to each item 
  // in the "formattedJSON" array
  return {
    title : item.project.title,
    description : item.project.description,
    city : item.project.city
  }

})

1 Comment

This is complicating things more than it should, item.project is already an Object, that you can return
0

The JSON.parse reviver parameter can be used to change or exlude values:

var j = '[{"id":"01","name":"Jane","project":{"title":"Alpha","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"New York"},"dateCreated":"2018-04-28 04:11:16 UTC"},{"id":"02","name":"Sarah","project":{"title":"Beta","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"London"},"dateCreated":"2018-04-28 04:11:29 UTC"},{"id":"03","name":"Mia","project":{"title":"Gamma","description":"Lorem ipsum dolor sit amet, consectetur adipiscing elit","city":"Paris"},"dateCreated":"2018-04-28 04:11:29 UTC"}]'

let result = JSON.parse(j, (k, v) => v.project || v);

console.log( result )

Comments

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.