Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

javascript array - display obejcts

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
nriddock
Asked:
nriddock
1 Solution
 
Ahmed HusseinCommented:
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
 
nriddockAuthor Commented:
Beautiful! That worked perfectly. Thanks!
0
 
leakim971PluritechnicianCommented:
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now