Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-28-2011, 02:28 PM   PM User | #1
DreamT
New to the CF scene

 
Join Date: Mar 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
DreamT is an unknown quantity at this point
Question Member names?

I have the following JSON object:

PHP Code:

var articles =     {
    
"ABC123" : { "brand" "Fruit of the loom""artgroup" "sweaters""colour" "black" },
    
"XYZ789" : { "brand" "Levis""artgroup" "pants""colour" "blue" }
            }; 
, and I want to filter out the article numbers (only one in this test case) where the brand is levis. In the function call, I.d. like to be able to use the property name as an argument, i.e.

PHP Code:
Filter("brand","Levis");

function 
Filter(searchFor,Property)
{
    for (var 
key in articles)
    {
         ...
        }

but, I don't know how to access the property name. How can I do it?
DreamT is offline   Reply With Quote
Old 03-28-2011, 02:42 PM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
You are dealing with objects whose properties' values are objects as well.
Code:
function Filter(searchFor,Property){
var sFor;
for(key in articles){
	sFor=articles[key][searchFor];
	if(sFor==Property){
	// do whichever, for instance:
	alert('Article: '+key);
	}
	
}
}
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Tags
javascript, json, property

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:19 PM.


Advertisement
Log in to turn off these ads.