0

Having trouble parsing A url object how I would like, still new to url strings would love some help. My object looks like this :

{mod1: "/hello/world", mod25: "/hey/jude/how/are/you"}

and I need to parse it into something like this

{
"mod1" : {"hello" : ["world"]},
"mod2" : {"hey" : ["jude","how","are","you"]}
}

How do I parse this url into an object like so? Thanks!

Edit: so far

  var parseObj = $location.search();

            _.each(parseObj, function(ob){
                console.log(ob.split());
            });

This gives me back the strings, however i am not sure how to now split them into an object where the key is the first item

3
  • Did you try anything ? What was the problem ? Commented Mar 10, 2015 at 15:37
  • 1
    so loop through the object and use split() Commented Mar 10, 2015 at 15:37
  • @dystroy stuck after the .split, unsure how to cut up the string into an object. Thanks! Commented Mar 10, 2015 at 15:43

3 Answers 3

1

Simple commented steps for you in vanilla JS:

// pass in the object to the function
function organise(obj) {

    // create a new object which we will return from the function
    var obj2 = {};

    // loop over the object we passed in
    for (var p in obj) {

        // create a temporary object
        var tmp = {};

        // split the value of current key/value pair into an array
        var arr = obj[p].split('/');

        // set the key of the temporary object to the second element
        // (because the first element of the array is an empty string)
        var key = arr[1];

        // add the rest of the array as the value of the new key
        // of the temporary object
        tmp[key] = arr.slice(2);

        // finally add the new temporary object as the value of the key
        // of the object we want to return
        obj2[p] = tmp;
    }

    // return the object from the function
    return obj2;
}

organise(obj);

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for taking the time to comment, it is unbelievable helpful for someone just starting this stuff like me :)
My pleasure. Helps me understand it better too.
0

How about this one?

var x = {mod1: "/hello/world", mod25: "/hey/jude/how/are/you"};

var result = {};

for(var i in x) {
  var entries = x[i].split('/');
  var firstEntry = entries[1];
  var arr = [];
  for(var j = 2; j < entries.length; j++) {
    arr.push(entries[j]);
  }
  var obj = {}
  obj[firstEntry] = arr;
  result[i] = obj;
}

console.log(result);

Comments

0

Loop over the object properties, split the values and insert them into a second object.

function parse(obj) {
  var out = {},
    parts;
  for (prop in obj) {
    if (obj.hasOwnProperty(prop)) {
      out[prop] = {};
      // replace the first forward slash with nothing so the first 
      // element of the array is not an empty string.
      parts = obj[prop].replace('/', '').split('/');
      // make the first element of the array the key and assign the 
      // rest of the array as the value.
      out[prop][parts.shift()] = parts;
    }
  }
  return out;
}

var obj = parse({
  mod1: "/hello/world",
  mod25: "/hey/jude/how/are/you"
});
// just display the object
document.write('<pre>' + JSON.stringify(obj, 0, 3));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.