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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Array conversion issue

    Hello friends,

    I am trying (in Javascript an Coldfusion) to convert: {"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"},
    into this: { member,book,journal,new_member,cds}

    Notice that I am trying to eliminate quotes.

    Is it possible to achieve that? How can I do it?

    Thanks
    Tom

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,917
    Thanks
    56
    Thanked 545 Times in 542 Posts
    hello,

    those are not arrays they are objects, and the format you are trying to achieve is not valid for an object (afaik, an object's properties must have values, even if they are null or an empty string). You can push the values onto an array like this:
    Code:
    <script type="text/javascript">
    var oldObj={"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"}
    var arr=[]
    
    for (var i in oldObj) {
    arr.push(oldObj[i])
    }; 
    </script>
    but note that they will still be strings (ie, wrapped in quotes), which is probably good anyway because otherwise they would be variables.

    maybe if you explained what you are trying to do you will get a suggestion for a better way to achieve it?

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for your reply. The reason that i need this ({member,book,journal,new_member,cds}) is that the search server i am using unfortunately consumes that kind of "datatype" (for querying). This obj: {"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"} is the response that I got from the client. So, I end up with this:
    Code:
    var tata={"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"}
    		var arr=[]
    	 
    	for (var i in tata) {
    		arr.push(tata[i])
    	}; 
    	
    	console.log(arr);
    	
    	
    	wrd = new Array(arr)
    		var joinwrd = wrd.join(",");
    	
    	console.log('{' + joinwrd + '}');
    Thank you again my friend


  •  

    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
    •