0

I have the following JSON array of objects :

"employees":[
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"}, 
    {"firstName":"Peter","lastName":"Jones"}
]

and i want to get a specific key in JSON array :

for (var ke in employees) {
       if (employees.hasOwnProperty(ke)) {
             console.log(employees[ke].lastName);
       };

but it does not work !!

5 Answers 5

2

You're missing a closing curly bracket at the end of your for loop, it should be:

for (var ke in employees) {
   if (employees.hasOwnProperty(ke)) {
         console.log(employees[ke].lastName);
   };
}

Demo

However, you could use angular.forEach instead, like this:

angular.forEach(employees, function (ke) {
    if(ke.lastName) {
        console.log(ke.lastName);
    }
});

Demo

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

Comments

2

When traversing through Javascript arrays, use the standard for loop rather than for...in.

for (var i=0; i < employees.length; i+=1) {
    console.log(employees[i].lastname);
}

Alternatively, ES5 now provides an Array.forEach function that could be used as well.

employees.forEach( function (employee) { 
  console.log(employee.lastname);
}

Comments

1

try this

for (var index in employees) {
    var tp = employees[index];
    console.log(tp['yourkeyhere']);
}

Comments

1

Change your code to the following. It works correctly.

employees=[
        {"firstName":"John", "lastName":"Doe"}, 
        {"firstName":"Anna", "lastName":"Smith"}, 
        {"firstName":"Peter","lastName":"Jones"}
    ]
    for(var ke in employees) {
           if (employees.hasOwnProperty(ke)) {
                 console.log(employees[ke].lastName);
           };
   }

Comments

1

Approach: 1 - With more validation.

var employees= [
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"}, 
    {"firstName":"Peter","lastName":"Jones"}
];

for(var obj in employees) {
    if(employees.hasOwnProperty(obj)){
        for(var prop in employees[obj]){
            if(employees[obj].hasOwnProperty(prop)){
                alert(prop + ' : ' + employees[obj][prop]);
                //to change the key's value
                if(employees[obj][prop] == 'Smith'){
                    employees[obj][prop]='Mike';
                } 
            }
        }
    }
}

Plunker

Approach: 2 Without validation (not recommended)

   for(i in employees) {
       var obj = employees[i];
       for(j in obj) {
           var key = j;
           var val = obj[j];
           alert(key + " : " + val);   
       }
    }

Edit:

var employees={"firstName":"John", "lastName":"Doe"};
Object.keys(employees); // return ["firstName", "lastName"]

Object has a prototype keys, returns an Array of keys in an Object

Chrome, FF & Safari supports Object.keys

3 Comments

You should consider decouple each for, you are having a complexity of 1n (1 for a lot of fors) you can create a map if you want to iterate trough objs inside an object, but in this case there are no need for doing that. a plain for with Object.keys should work perfectly.
you want to change key or value for lastName?
i want to change value

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.