Solved

javascript array - display obejcts

Posted on 2013-05-31
3
306 Views
Last Modified: 2013-05-31
Hello,

I have the following code to search through an array for a specific ID.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <script src="jquery.js" type="text/javascript"></script>
  <script type="text/javascript">
    myArray = [{'id':'73','foo':'bar','my':'val'},{'id':'45','foo':'bar','my':'val'},{'id':'71','foo':'bar','my':'val'},{'id':'33','foo':'bar','my':'val'},{'id':'22','foo':'bar','my':'val'}];

	var found = $.map(myArray, function(val) {
	    return val.id == '33' ? val.foo : null;
	});

	document.write(found.length + "\n" + found[0]);
  </script>
 </head>
</html>

Open in new window


It currently outputs only the ID and one object... "1 bar"

What do i need to do to output the values for ID, foo, my?

Thanks!
0
Comment
Question by:nriddock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Accepted Solution

by:
Ahmed Hussein earned 500 total points
ID: 39210982
Try this:

myArray = [{'id':'73','foo':'bar','my':'val'},{'id':'45','foo':'bar','my':'val'},{'id':'71','foo':'bar','my':'val'},{'id':'33','foo':'bar','my':'val'},{'id':'22','foo':'bar','my':'val'}];

	var found = $.map(myArray, function(val) {
	    return val.id == '33' ? val.id +" "+ val.foo +" "+ val.my : null;
	});

	document.write(found);

Open in new window


~AG
0
 

Author Closing Comment

by:nriddock
ID: 39211005
Beautiful! That worked perfectly. Thanks!
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39211013
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

632 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