I am trying to break down a string into an array. This code below does do it, but I now need to break it down into a either a sub array.
Here is the String and the Code:
$(document).ready(function() {
var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}';
var result = content.split('}');
result.pop();// removing the last empty element
console.log(result);
for(var i=0;i<result.length;i++)
{
result[i]+='}';
console.log(result);
$('div').append('<li>' + result[i] + '</li>');
}
})
This out puts:
<li>Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}</li>
<li>Button{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}</li>
<li>Button{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}</li>
<li>Label{Position: 106, 91;Width: 96;Height: 34;Text: "Button";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}</li>
What I need to do now is break it down even further so that I have the word prior to {} i.e Image for the first one
Ideally I would like the output to be a key/value object like this
{
"Controls": [{ "Image":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
"Button":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
"Button":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
"Label":"{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}", }],
}
My main goal is to be able to target either Key or the Value at the end of this.
Any help is appreciated
This should produce the output you want:
Working Demo: http://jsfiddle.net/fewds/TuNTV/2/
Output Example:
If you just want to have multiple key occurrences increment you could use this:
Working Demo: http://jsfiddle.net/fewds/TuNTV/5/
Output Example: