Solved

javascript array - display obejcts

Posted on 2013-05-31
3
305 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating number not work with decimal 4 46
Angular2 remove duplicates 11 24
hide and show spans from dropdown selection 3 38
Javascript function 3 20
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

734 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