Solved

javascript array - display obejcts

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now