1

How can I find "firstname" with value "Anna" and display her "lastname" at the end using JavaScript? This is just a dummy example for me to understand how it works.

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

document.getElementById("demo").innerHTML =
employees.firstName;
<p id="demo"></p>

5 Answers 5

1

You'd have to iterate and check for that value, then do what you want:

for (var i = 0; i < employees.length; i++) {
    if (employees[i].firstName == "Anna") {
        document.getElementById("demo").innerHTML = employees[i].lastName;
        break;
    }
}

To display more than 1 Anna - I'd use filter on the array, and then join:

var lastNames = employees.filter(function(employee) {
    return employee.firstName == "Anna";
}).map(function(employee) {
    return employee.lastName;
}).join(",");

document.getElementById("demo").innerHTML = lastNames;
Sign up to request clarification or add additional context in comments.

5 Comments

For some older browsers, you can not get an Object length. But if you had an Array instead of an Object i would suggest using this method. but loop backward instead of forward.
@RichardGrant -- This is an array - the above code never uses an object length. And why loop backwards instead of forwards?
Woops i didnt really look at the code to much your right. your code would work. i apologize. when you loop forward your loop needs to constantly keep checking the item length causing the loop to run much slower. there is plenty of documentation about this.
@RichardGrant -- Idk about much slower, the performance gain is extremely minimal - no need to optimize so soon.
Great! Thanks a lot. Just a final question, what if I have more then one "Anna", how can I display all of their "lastNames" in a row?
0

I've done something similar like this

var quality = function(obj) {
  if (obj.quality === 'source') {
    return obj;
  }
};

context.download.filter(quality)[0].link;

This filter should return each object that matches the filter in a new array.

Comments

0

For more complex queries you can also use a JSON query language such as JSON Query (surprise!)

var jsonQuery = require('json-query')

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

jsonQuery('employees[firstName=Anna].lastName', {
  data: data
})

Comments

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

for (var i = e.length - 1; i >= 0; i--) {
    if (e[i].firstName == "Anna") {
        console.log(e[key]['firstName'] + ', ' + e[key]['lastName']);
        break;
    }
};

You need to loop through the Object And find the value for the key you are looking for. http://jsfiddle.net/mjs2avag/

3 Comments

Don't use the for in syntax with arrays, it's meant to iterate object properties.
There is no reason to believe that this type of for loop is for Arrays only. The only difference is that the for loop will capture the item prototype.
0

Iterate through the first array and use a condition to find the value 'anna' , then access it like employees[i].lastName . Here is the working example. JSFIDDLE LINK

Html

 var employees = [{
            "firstName": "John",
                "lastName": "Doe"
        }, {
            "firstName": "Anna",
                "lastName": "Smith"
        }, {
            "firstName": "Peter",
                "lastName": "Jones"
        }];
        
       
       for (var i in employees) {
             if (employees[i].firstName == 'Anna') {
                
                document.getElementById("demo").innerHTML = employees[i].lastName;
        
            }
        
        
        
        }
 <h3 id="demo"></h3> <!--This one will print smith Anna's lastname -->


    

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.