www.webdeveloper.com
Results 1 to 10 of 10

Thread: Transforming a string into an object

  1. #1
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319

    Transforming a string into an object

    Hello,

    Does anybody knows an efficient way to go from ...

    var ns = this is a namespace

    ... to this ...

    var ns = ['this']['is']['a']['namespace']

    ?

    Thanks in advance!

    Christophe

  2. #2
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    374
    result = "this is a namespace".split(" ")

    will create result as an array with four elements.

  3. #3
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319
    Yes, I know, but that will create an one array with four properties. I need a an array, in an array, in an array ....

    split() will create ['this', 'is', 'a', 'namespace'] instead of ['this']['is']['a']['namespace']
    Last edited by Christophe27; 07-12-2012 at 04:14 PM.

  4. #4
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319
    Anybody? Help is very appreciated!

  5. #5
    Join Date
    Mar 2012
    Posts
    202
    Give me a second lol

  6. #6
    Join Date
    Mar 2012
    Posts
    202
    Code:
    var ns = "this is a namespace".split(" ");
    var temp = new Array();
    for(i=0;i<ns.length;i++) { temp[i] = new Array(ns[i]); }
    Then just use temp

  7. #7
    Join Date
    Mar 2012
    Posts
    202
    May I ask why exactly you need to do such a redundant task?

  8. #8
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319
    Hi Gray,

    Thanks for your reply!

    Your result gave: [['this']['is']['a']['namespace']], not the desired structure I was hoping for ...



    I'm setting up a node server using socket.io. On the node server, I use the following namespace:

    Code:
    GLOBAL = {
    
        dialog: {
    
            settings: function() {},
            profile: function() {}
    
        }
    
    } // With much, much more ...
    With socket.io you invoke a function of the backend, from the front end.

    Front end:
    Code:
    io.emit('getSettings', {}, function() {});
    These functions are defined on the backend like follows:
    Code:
    io.on('getProfile', function() {
        // Get profile from the db
    });
    But this way, I have to define many different functions, one af the other. In other words, one big unstructured mess Therefore I have put the content of these functions in a namespace (consistent with the namespace of the application itself)

    Like the following:

    Front end:
    Code:
    io.emit('message', {'method: 'this is a namespace'}, function() {});
    Back end:
    Code:
    io.on('message', function(params) {
        Namespace to the desired function ...
    });
    This way you only have to define a funcion once, with one method called 'message', and then in the body 'namespace' to a particular function.

    Anybody?

    Christophe

  9. #9
    Join Date
    Jan 2010
    Location
    Belgium
    Posts
    319
    And at the moment I use this mess:

    Code:
    var ns = params.namespace.split(' ');
         params["clientIP"] = socket.handshake.address.address;
    
    
    if( typeof ns[1] === 'undefined' ) {
        GLOBAL[ns[0]](params,function (result) {fn(result)});
    } else if( typeof ns[2] === 'undefined' ) {
        GLOBAL[ns[0]][ns[1]](params,function (result) {fn(result);});
    } else {
        GLOBAL[ns[0]][ns[1]][ns[2]](params,function (result) {fn(result);});
    }
    Who can make this better ))

  10. #10
    Join Date
    Mar 2012
    Posts
    202
    If you are trying to get this:
    Code:
    ['this']['is']['a']['namespace']
    Yet apparently this is unacceptable:
    Code:
    [['this']['is']['a']['namespace']]
    Then I don't understand what you are asking.

    As for the rest of your code, I have no idea what it's actually doing....

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles