5

Learning how to access property values.

If I have

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];

How would I console.log just the value of the second object? I.e. "website developer".

I know how to console.log the entire key-value pair (or object) by using .find():

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];

console.log(object1.find(function(element) {
    return element.hasOwnProperty("job");
}
));

But what about just the value of this pair?

2
  • Why do you have each property in its own object, instead of a single object like {name: "HappyHands31", job: "website developer", ...}? Commented May 29, 2019 at 23:18
  • @Barmar this was just the way it was presented in a coding challenge... Commented May 29, 2019 at 23:19

6 Answers 6

4

You can acces items in arrays at given position by their index. In javascript indexes of arrays are starting with 0: myArray[0]. To access the property of the returned object just use dot-notation: myArray[0].myProperty.

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];

console.log(object1[1].job);

For your given example this can also achieved by appending the property-name (with dot-notation):

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];

console.log(object1.find(function(element) {
    return element.hasOwnProperty("job");
}).job);

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

1 Comment

Sorry I edited the values of "name" and "age" : ) In the second snippet
2

You can destructure the value:

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];
const { job: res } = object1.find(({ job }) => job);
console.log(res);

Comments

1

A solution would be to store somewhere the keys of each object and then access the object the usual way.

    let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];
    
    for(let i=0; i<3; i++){
       name = Object.keys(object1[i]);
       console.log(object1[i][name])
    }

In this case I've assumed that you don't know the name of the key you want to access.

Comments

1

Just add a property access to the end of it.

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];

console.log(object1.find(function(element) {
    return element.hasOwnProperty("job");
}).job);

Comments

1

First, your object1 is an array of objects, not an object.

Although, there's a way to achieve what you want.

const fun = (arr, prop) => arr.find((obj) => obj.hasOwnProperty(prop))[prop];

const object1 = [{name: 'HappyHands31'}, {job: 'website developer'}, {city: 'Chicago'}];

const property = 'job';

console.log(fun(object1, property));

Comments

1

If the object structure is the same in the array, you can flatten that array of objects like this and call it directly as object keys.

let object1 = [{name: "HappyHands31"}, {job: "website developer"}, {city: "Chicago"}];
let flatten = {}
object1.map((currentValue, index) => {
  let key = Object.keys(currentValue)  
  flatten[key] = currentValue[key]
});
console.log(flatten)
console.log(flatten.job)

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.