Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Member names?

  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •