Solved

AngularJS - how to query JSON and return a value

Posted on 2015-01-08
4
3,413 Views
Last Modified: 2015-01-09
What is the proper way to query json and return a value?  Can you please provide an example?

How can I query Employees - find firstname = "Peter" and return "Jones"

{"employees":[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}
0
Comment
Question by:JElster
  • 2
4 Comments
 
LVL 16

Assisted Solution

by:Imran Javed Zia
Imran Javed Zia earned 50 total points
ID: 40539750
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 40539753
And from jQuery you can use following:

http://api.jquery.com/jQuery.grep/
0
 
LVL 44

Assisted Solution

by:Rainer Jeschor
Rainer Jeschor earned 50 total points
ID: 40539846
Hi,
sorry I am currently very limited in time, but the following two tutorials should shade in some lights:
http://blog.hfarazm.com/filters-in-angularjs/
http://blog.hfarazm.com/data-binding-angularjs/
HTH
Rainer
0
 
LVL 22

Accepted Solution

by:
ambience earned 400 total points
ID: 40539913
Getting java object from JSON string is as simple as

var employees = angular.fromJson(jsonStr);

As for finding an item in the returned object, and doing it using angularJS,

    angular.forEach(employees, function(value) {
       if(value.firstName == name) this.push(value);
    }, employees);

However, doing it the angular way is to write a filter for that

This should help: http://jsbin.com/cawesiqeqi/1/edit?html,js,output
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
suddenly angular cli doesn't work. 5 42
What if i make webbased alternative for MS Access 5 72
Filktering Alphabetically 8 32
Wordpress Pagination 1 30
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question