1

I am trying to pull a specific JSON object from an array of objects using JavaScript. Here is my JSON:

"Awards": [
   {
     "Award1": {
        "title": "Award1 Title",
        "recipient": "John Doe",
        "description": "Blah Blah Blah"
     }
   },
   {
     "Award2": {
        "title": "Award2 Title",
        "recipient": "Tom White",
        "description": "Blah Blah Blah"
     }
   },
   {
      "Award3": {
         "title": "Award3 Title",
         "recipient": "Will Biggs",
         "description": "Blah Blah Blah"
      }
   }
]

What I want to be able to do is create a function that takes in the data and an id, finds the object in the array and returns the object as a whole. For example if I search for Award1, I want it to return:

var obj = {
 "title": "Award1 Title",
 "recipient": "John Doe",
 "description": "Blah Blah Blah"
}

So then I could access the data like so:

obj.recipient // Which would return John Doe

Ideas?

2 Answers 2

2

Finds the first object that contains the property key=Award1 and returns it.

const findKey = (list, key)=>(list.find(obj=>obj[key])||{})[key]

data = {
"Awards": [
   {
     "Award1": {
        "title": "Award1 Title",
        "recipient": "John Doe",
        "description": "Blah Blah Blah"
     }
   },
   {
     "Award2": {
        "title": "Award2 Title",
        "recipient": "Tom White",
        "description": "Blah Blah Blah"
     }
   },
   {
      "Award3": {
         "title": "Award3 Title",
         "recipient": "Will Biggs",
         "description": "Blah Blah Blah"
      }
   }
]
}

console.log(data.Awards.find(obj=>obj["Award1"]))

let key = "Award1"
console.log(data.Awards.find(obj=>obj[key])[key].title)

console.log(
findKey(data.Awards,'Award1')
)

console.log(
findKey(data.Awards,'Award2')
)

console.log(
findKey(data.Awards,'Award3')
)

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

2 Comments

This answer is close, the issue is that once its extracted, it is an object within and object. thus I cant say for example: obj.title, because it it would be looking for an field called title, but there is only one field in the object and that is the id of another object. (Something like obj.[key].title doesn't exist, at least to my knowledge)
obj[key].title or Object.values(obj)[0].title or obj[Object.keys(obj)[0]].title
1

This should do the trick:

var obj=Awards['Award1'];

But keep in mind that this will only be a referenced copy, i. e. changes to obj will also happen to the original Awards object.

There is not really a need to create a function for it, but of course, you can do that too:

function getObj(parent,key){
  return parent[key];
}

var obj=getObj (Awards,'Award1');

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.