SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    May 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by anarchos78 View Post
    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?
    When you define an object, you need to associate the key and value in some way, so objects cannot work as you want there.

    Currently the object you're starting with would have its values access as obj.val1, obj.val2, etc...
    Let's start with finding out what data structure you are wanting, and why are you wanting to eliminate quotes?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,707
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by anarchos78 View Post
    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"}
    Ahh, so you want to end up with a string representation of the object values, right.

    That can be done now in modern web browsers with:

    Code javascript:
    var obj = {"val1": "member", "val2": "book", "val3": "journal", "val4": "new_member", "val5": "cds"},
        keys = Object.keys(obj),
        objValue = function (key) {
            return obj[key];
        },
        values = Array.prototype.map.call(keys, objValue);
    console.log('{' + values.join(', ') + '}');

    But the code you had there also does the same job.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Member
    Join Date
    May 2011
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks my friend


Tags for this Thread

Bookmarks

Posting Permissions

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