Devmix
Devmix

Reputation: 1848

Loop over an object

I'm decoding an object and so far I got it working. Let's say I have this object:

var person = [{
   firstname: "Mike",
   lastname: "123ñññ"
   age: 20
}]

So in order to decode &ntilde and render ñ, I'm simply doing this:

new DOMParser().parseFromString(person[0].lastname, "text/html").documentElement.textContent;

and this will render the value

ñññ

which is what I want, so it will look like this:

lastname: "ñññ"

However, the issue that I'm facing is that I need to decode values for each property in the object because I may get those special characters for firstname or other properties. So my question is how to decode property values on an object assuming that the object may look like this:

var person = [{
   name: "Mike",
   lastname: "123ñññ"
   age: 20,
   employeer: {
     name: 'ABC Company ñê',
     supervisors:[
         {
           name: 'Steveä',
           code: 'è468'
         }
     ]
   }
}]

NOTE:

I don't need help on decoding that values of each property on my object, since I'm already doing that, I just need to come up with a recursive function that will do that on a nested object

Upvotes: 1

Views: 108

Answers (3)

Kubwimana Adrien
Kubwimana Adrien

Reputation: 2531

Try this:

function decodeObject(obj){
  if(typeof obj == 'object'){
    var text = JSON.stringify(obj),
      div = document.createElement('div');
      div.innerHTML = text;
      obj = JSON.parse(div.childNodes[0].nodeValue);
  }

  return obj;
}


var person = decodeObject([{
   name: "Mike",
   lastname: "123ñññ",
   age: 20
}]);



console.log(person);

Upvotes: 0

gdanov
gdanov

Reputation: 311

in functional languages there are libraries to recursively walk tree scructures. In clojure there is the zipper and walk for example.

you could write it yourself but it will quickly get complicated so I suspect using JSON.stringify + parse would give you what you need. Both functions take second argument that's replacer and reviver respectively that allow you to intercept the transformations and alter the values.

Here is example from the official documentation:

function replacer(key, value) {
  // Filtering out properties
  if (typeof value === 'string') {
    return undefined;
  }
  return value;
}

var foo = {foundation: 'Mozilla', model: 'box', week: 45, transport: 'car', month: 7};
JSON.stringify(foo, replacer);
// '{"week":45,"month":7}'

Upvotes: 0

Wyck
Wyck

Reputation: 11730

I think a recursive decode using DOMParser is a good idea. Here's an in-place transformer. Perform a deep copy first and then transform in-place if you prefer.

var person = [{
   name: "Mike",
   lastname: "123ñññ",
   age: 20,
   employer: {
     name: 'ABC Company ñê',
     supervisors: [
         {
           name: 'Steveä',
           code: 'è468'
         }
     ]
   }
}];
console.log(person);

function htmlDecode(input)
{
  var doc = new DOMParser().parseFromString(input, "text/html");
  return doc.documentElement.textContent;
}

function fix(obj) {
   for (let prop in obj) {
      switch (typeof obj[prop]) {
        case 'object':
          fix(obj[prop]);
          break;
        case 'string':
          obj[prop] = htmlDecode(obj[prop]);
          break;
      }
   }
}

fix(person);
console.log(person);

Upvotes: 1

Related Questions