0

I am trying to translate the list of string into a literal object in javascript, i tried using eval but unfortunately it doesn't work for me or maybe i implemented it wrong

This is my sample list

var listOfTempData = [];

listOfTempData.push("retVal.Rate='125.4'");
listOfTempData.push("retVal.OTRateBasis='Current'");
listOfTempData.push("retVal.Name='Holiday OT'");
listOfTempData.push("retVal.Code='HOT101'");
listOfTempData.push("retVal.OvertimeRateType.Code='C72310AE-6'");

and i want to create a literal object like this

{
  Rate: '125.4',
  OTRateBasis: 'Current',
  Name: 'Holiday OT',
  Code: 'HOT101',
  OvertimeRateType: {
    Code: 'C72310AE-6'
  }
}

based on the list of strings from listOfTempData

This is what i've done so far

var retVal = {};

for(var i = 0 ; i < listOfTempData.length; i++)
{
    eval(listOfTempData[i]);
}

any help please.

8
  • 5
    A JSON format string would make this trivial, can you change the format? Commented Apr 7, 2015 at 16:38
  • 4
    Before you can assign to retVal.OvertimeRateType.Code you have to do retVal.OvertimeRateType = {}. Commented Apr 7, 2015 at 16:40
  • 9
    A wise man once told me: "If you're using eval, you're most likely doing it wrong." Commented Apr 7, 2015 at 16:40
  • Instead of using eval, parse the template strings. You can then use a loop to assign to the nested properties. Commented Apr 7, 2015 at 16:41
  • @Alex K. i wish i can but that list of string is generated via code or is already defined :( Commented Apr 7, 2015 at 16:42

1 Answer 1

1

This will do what you're asking for, with the example code supplied...

var listOfTempData = [];

listOfTempData.push("retVal.Rate='125.4'");
listOfTempData.push("retVal.OTRateBasis='Current'");
listOfTempData.push("retVal.Name='Holiday OT'");
listOfTempData.push("retVal.Code='HOT101'");
listOfTempData.push("retVal.OvertimeRateType.Code='C72310AE-6'");

var obj = {};

function addProperty(obj, name, value) {
    if (name.indexOf(".") == -1) {
        obj[name] = value;
    }
    else {
        var name1 = name.split(".")[0];
        var name2 = name.substr(name.indexOf(".") + 1);
        obj[name1] = {};
        addProperty(obj[name1], name2, value);
    }
}

for(var i = 0; i < listOfTempData.length; i++) {
    var data = listOfTempData[i];
    data = data.substr(data.indexOf(".") + 1).split("=");
    addProperty(obj, data[0], data[1]);
}

console.dir(obj);

As you can see, it removes retVal. from each string and then splits by = in order to get the name and value. With that information it's simple to create an object that represents the data. It is also recursive so that it finds delimiters in names and splits accordingly.

Here's a working fiddle...

http://jsfiddle.net/ArchersFiddle/zxsaqjf3/1/

Just run it and check the console for output.

Incidentally, I hate doing things like this - it makes my skin crawl. All it would take is one change at the data source and your whole application could collapse. If there is any way possible to fix the data source, rather than massage it later, then I would strongly recommend doing that.

Just my 2 cents :)

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

2 Comments

Thanks sir, i think this is better solution than using eval
I could not agree more, and I'm glad I could help :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.