0

I have an array of strings in object form received from file, only I need to add quotation mark around parameter names of objects which are strings inside an array and around their values between square brackets to convert those strings to proper objects.

["{Durdham Hall Electric: [e4.kwh]}", "{University Hall Substation: [e1.kwh]}", 
"{University Hall Substation: [e2.kwh]}"]

I have no idea how to loop through values and to add required symbol in required part.

6
  • is this coming from a json file? Commented Aug 19, 2015 at 16:35
  • 1
    Wouldn't it be easier to supply yourself with proper JSON (ie JSON where the objects aren't quoted, but the keys/values are)? Commented Aug 19, 2015 at 16:36
  • 2
    Consider fixing the source / how you get the data. You can't even eval those strings. Commented Aug 19, 2015 at 16:37
  • Data coming from one javascript file to another javascript file in array form. Those values are obtained from option element value, so there is no way of sending them in other form Commented Aug 19, 2015 at 16:37
  • I bet you there is. What code are you using to get the option element values into the array? Commented Aug 19, 2015 at 16:39

3 Answers 3

1

Maybe change

options.push('<option value="' + '{' + data[devices][1] + ': ' + '[' + 'e' + i + '.kwh' + ']' + '}' + '" >' + meterName + '</option>')

to something like this, then you get a little nice parsable JSON

var data = [[0, 'Durdham Hall Electric:']],
    devices = 0,
    meterName = 'meterName',
    i = 3,
    options = [];

options.push('<option value="' + '{ \\"device\\": \\"' + data[devices][1] + '\\", \\"kwh\\": \\"' + 'e' + i + '\\"}' + '">' + meterName + '</option>');

alert(options);

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

Comments

1

You can use Regex and forEach to do this:

var data = ["{Durdham Hall Electric: [e4.kwh]}", "{University Hall Substation: [e1.kwh]}", 
"{University Hall Substation: [e2.kwh]}"];

data.forEach(function(v,i){
  data[i] = JSON.parse( v.replace(/{(.+):\s\[(.*)\]}/g, '{"$1":["$2"]}') );
});

console.log(data); // Open your console to see the results

Comments

0

If the strings are always starting with a { you can use substring and then combine the string back together.

String part1;
String part2;
String result;

String str = "{Durdham Hall Electric: [e4.kwh]}"

Then use index of to find the :

part1 = str.subString(1, str.indexOf(":"));
part2 = str.subString(str.indexOf(":"), str.length());
result = "{\"" + part1 + "\"" + part2;

Believe something like this could work however you do have to make some assumptions. This is only for a single element so you would have a loop for each item in your string array.

Comments

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.