3

I want to access the keys of the the objects in the array.

The example of object is:

var trackobj = {
        trackees: [{
            john: {
                "lat": "120000"
                , "long": "12345"
            }
            , harry: {
                "lat": "210000"
                , "long": "5433333"
            }
            , sid: {
                "lat": "420000"
                , "long": "21345"
            }
    }]
    }

I am using the function:

function searchTrackee() {
        for (var i = 0; i <= trackobj.trackees.length; i++) {
            console.log(trackobj.trackees[i]['key']);
        }
    }

The expected output I expect is "john" "harry" "sid".

4 Answers 4

4

I think the following should help you.

function searchTrackee() {
   trackobj.trackees.forEach(function(obj) {
      console.log(Object.keys(obj));
   });
}

EDIT 1:

I also agree with @MedAli. It is a good practice to use map and store it. Then print separately.

function searchTrackee() {
   var output = trackobj.trackees.map(function(obj){
       return Object.keys(obj);
   });
   console.log(output);
}
Sign up to request clarification or add additional context in comments.

Comments

3

you can do it with a simple for...in statement like this:

for (var i = 0; i <= trackobj.trackees.length; i++) {
    for(var key in trackobj.trackees[i]) {
        console.log(key);
    }
}

Comments

2

You can do something like:

trackobj["trackees"].map(function(a){
 return Object.keys(a)
})

So you can update your code, as follows:

function searchTrackee() {
    var results = trackobj["trackees"].map(function(a){
     return Object.keys(a)
    })
    console.log(results)
}

The output is an array with the follows values:

["john","harry","sid"]

var trackobj = {
        trackees: [{
            john: {
                "lat": "120000"
                , "long": "12345"
            }
            , harry: {
                "lat": "210000"
                , "long": "5433333"
            }
            , sid: {
                "lat": "420000"
                , "long": "21345"
            }
    }]
    }
    
function searchTrackee() {
        var results = trackobj["trackees"].map(function(a){
         return Object.keys(a)
        })
        console.log(results)
}

searchTrackee(); 

Comments

0

You can also use Array#reduce to get all names.

var trackobj = {trackees: [{
            john: {
                "lat": "120000"
                , "long": "12345"
            }, harry: {
                "lat": "210000"
                , "long": "5433333"
            }, sid: {
                "lat": "420000"
                , "long": "21345"
            }}]
}

var names = trackobj['trackees'].reduce((names,obj) =>  names.concat(Object.keys(obj)),[]
);
console.log(names);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.