Abhishek Singh
Abhishek Singh

Reputation: 426

Modify the values of each JSON objects

I was working with this JS file which returns the values in JSON format. I need to have a member function append @ at beginning and end of each string values. I can't figure out how to do that. The content of file is like this, but may have any number of objects

var SomeText = function () {
    return {
        "CommonText":
        {
            "ViewAll": "View All",
            "SignOut": "Sign out",
            "More": "More",
            "New": "New"
        },
        "ErrorText": {
            "Tag": "Error !",
            "EmptyGridMessage": "You do not have any {0}"
        },
    };
};

Here I need to append @ at each string value. E.g. for Tag/Error name value pair I need to convert it like "@Error !@".

Upvotes: 1

Views: 62

Answers (3)

Andy
Andy

Reputation: 63524

You could try something like this. It will only change the first level as indicated by your question:

// get the object returned by SomeText
var output = SomeText();

// for each object property rewrite the value
// using the updateObject function
Object.keys(output).forEach(function (obj) {
  output[obj] = updateObject(output[obj]);
});

function updateObject(obj) {

  // for each property in the object, update the value
  Object.keys(obj).forEach(function (el) {
    obj[el] = '@' + obj[el] + '@';
  });
  return obj;
}

DEMO

Upvotes: 1

cezar
cezar

Reputation: 12012

Here is my solution, using a recursive function to add @ only to strings and iterate through objects.

function appendAt (text) {
    for (var t in text) {
        if (text.hasOwnProperty(t) && typeof text[t] === "string") {
            text[t] = '@' + text[t] + '@';
        }
        if (typeof text[t] === "object") {
            appendAt(text[t]);
        }
    }
    return text;
}

// run the function
var text = SomeText();
console.log(text); // normal output
appendAt(text);
console.log(text); // output with appended @ at begin/end of each string

Upvotes: 1

Black0ut
Black0ut

Reputation: 1737

Here is a generic way to do so using recursion, unlike the other solutions it will change all the strings in the object no matter in what level they are :

  var myObj = {
        "CommonText":
        {
            "ViewAll": "View All",
            "SignOut": "Sign out",
            "More": "More",
            "New": "New"
        },
        "ErrorText": {
            "Tag": "Error !",
            "EmptyGridMessage": "You do not have any {0}"
        },
    };
    function deepStringConcat(myObj) {
        function walker(obj) {
            var k, has = Object.prototype.hasOwnProperty.bind(obj);
            for (k in obj) if (has(k)) {
                switch (typeof obj[k]) {
                    case 'object':
                        walker(obj[k]); break;
                    case 'string':
                        obj[k] = '@' + obj[k] + '@';
                }
            }
        }
        walker(myObj);
    };
    deepStringConcat(myObj);
    console.log(myObj);

Fiddle

Upvotes: 3

Related Questions