Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9046057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:32:40+00:00 2026-06-16T11:32:40+00:00

I am trying to break down a string into an array. This code below

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:32:41+00:00Added an answer on June 16, 2026 at 11:32 am

    This should produce the output you want:

    $(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
        var obj = {Controls:{}};
    
        $.each(result, function (i, v) {
            var key = v.split('{');
            var value = v.replace(key[0], '') + '}';
    
            if (key[0] !== 'Button') {
                obj.Controls[key[0]] = value;
            } else {
                if (!obj.Controls.hasOwnProperty('Buttons')) {
                    obj.Controls['Buttons'] = [];
                }   
                obj.Controls.Buttons.push(value);
            }
        });
    
        console.log(obj);
    });​
    

    Working Demo: http://jsfiddle.net/fewds/TuNTV/2/

    Output Example:

    {
        "Controls": {
            "Image": "{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
            "Buttons": [
                "{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}",
                "{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;}"
        }
    }
    

    If you just want to have multiple key occurrences increment you could use this:

    $(document).ready(function() {
        var content='Image{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}Image{BackgroundImage: Image2.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;}Button{BackgroundImage: Button3.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;}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
        var obj = {Controls:{}};
    
        function nextProp(key) {
            if(obj.Controls.hasOwnProperty(key)) {
                var num = key.match(/\d+$/);
                if (num) {
                    return nextProp(key.replace(num[0], '') + (parseInt(num[0], 10) + 1));
                } else {
                    return nextProp(key + '1');
                }
            }
    
            return key;
        }
    
        for (var i = 0; i < result.length; i++) {
            var key = result[i].split('{');
            var value = result[i].replace(key[0], '') + '}';
            obj.Controls[nextProp(key[0])] = value;
        }
    
        console.log(obj);
    });​
    

    Working Demo: http://jsfiddle.net/fewds/TuNTV/5/

    Output Example:

    {
        "Controls": {
            "Image": "{BackgroundImage: Image.gif;Position: 0, 0;Width: 320;Height: 480;}",
            "Image1": "{BackgroundImage: Image2.gif;Position: 0, 0;Width: 320;Height: 480;}",
            "Button": "{BackgroundImage: Button.gif;Position: 49, 80;Width: 216;Height: 71;}",
            "Button1": "{BackgroundImage: Button2.gif;Transition: View2;Position: 65, 217;Width: 188;Height: 134;}",
            "Button2": "{BackgroundImage: Button3.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;}",
            "Label1": "{Position: 106, 91;Width: 96;Height: 34;Text: \"Button\";FontSize: 32;Color: 0.12549, 0.298039, 0.364706, 1;}"
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New programmer here, I am trying to understand and break down this code below
I'm trying to break down this string into 2 colours and wondered if there
I'm Trying to get a php string to break down into individual words, search
I have the vector output = PV_out(:); I am trying to break this down
i'm trying to get this program to break down a user defined amount of
I am tring to paint on my own view (R.id.view) but this code does
Here's my url string, I am trying to break down the parameters and get
I am trying to break down the following string: @command Text1 @command2 Text2 in
I am trying to break some of my old habits of writing bad code.
I am trying to break a huge SQL file into little sql file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.