Splitting multiple strings

Posted on

Problem

I’m sure there’s a better way to approach the following. I’m writing a plugin where users can enter settings in the following format:

Setting: “object1setting -> object2setting”

It’s done this way to allow for multiple objects to be passed within the same JS call. I’m breaking the the strings apart at the “->” symbol and saving them, however there are many settings and I’m thinking there has to be a more efficient way of writing this:

// START PLUGIN, SETUP DEFAULT OPTIONS, EXTEND OPTIONS TO "O" .....
// I'VE JUST PULLED SOME RANDOM SETTINGS AS AN EXAMPLE. 

bg_x_speed_in_set = o.bg_x_speed_in.toString().split("->"), // X PROPERTY SPEED IN
bg_x_speed_out_set = o.bg_x_speed_out.toString().split("->"), // X PROPERTY SPEED OUT
bg_y_speed_out_set = o.bg_y_speed_out.toString().split("->"), // Y PROPERTY SPEED OUT

...
// ETC

This continues on and gets lengthy. Everything works perfectly in all browsers. I’m just trying to clean up the code.

Solution

I think you can use object literals. The user of your plugin can pass in an array of object literals:

var settings = [
    {bg_x_speed_in: ..., bg_x_speed_out_set : ...}
    , {bg_x_speed_in: ..., bg_y_speed_out_set : ....} 
    , {bg_x_speed_in: ..., bg_x_speed_out_set : ....} 
]; 

In your code, loop through the array; each element contains the settings for one object.

Leave a Reply

Your email address will not be published. Required fields are marked *