arrays - Most efficient way to parse a complex string in Javascript -
i'm having trouble parsing string.
newparams=[ "3","2","img1x:51","img1y:111","img1h:209","img1w:330","img1px:231","img1py:291","img1dx:44","img1dy:104","img2x:51","img2y:331","img2h:100","img2w:329","img2px:274","img2py:408","img2dx:44","img2dy:324","txt1x:399","txt1y:119","txt1h:103","txt1w:303","txtbox1x:391","txtbox1y:111","txtbox1h:119","txtbox1w:319","txt1px:679","txt1py:199","txt1dx:392","txt1dy:112","txt2x:399","txt2y:249","txt2h:103","txt2w:303","txtbox2x:391","txtbox2y:241","txtbox2h:119","txtbox2w:319","txt2px:679","txt2py:329","txt2dx:392","txt2dy:242","txt3x:399","txt3y:379","txt3h:44","txt3w:304","txtbox3x:391","txtbox3y:371","txtbox3h:60","txtbox3w:320","txt3px:680","txt3py:409","txt3dx:392","txt3dy:372"]; the string contains height, width, x , y coordinates of both text objects , image objects need position , resize in dom.
the first 2 values in array number of textfields, , number of images (which going use in loops max number of iterations).
i need parse number values, , push them in these individual arrays.
var textxcoords = new array(); var textycoords = new array(); var textheight = new array(); var textwidth = new array(); var txtboxxcoords = new array(); var txtboxycoords = new array(); var txtboxheight = new array(); var txtboxwidth = new array(); var textpullxcoords = new array(); var textpullycoords = new array(); var textdragxcoords = new array(); var textdragycoords = new array(); var imgxcoords = new array(); var imgycoords = new array(); var imgheight = new array(); var imgwidth = new array(); var imgpullxcoords = new array(); var imgpullycoords = new array(); var imgdragxcoords = new array(); var imgdragycoords = new array(); for instance in "img1x:51" (image 1, y coord), number 51 pushed imgxcoords array @ position 0.
and in "img2y:111" (image 2, y coord), number 111 pushed imgycoords array @ position 1.
and in "txt2w:303" (textfield 2, width), number 303 pushed txtboxwidth array @ position 1.
if can show me how push img(i)x , img(i)y values arrays (imgxcoords , imgycoords) using loop, can figure out rest there.
i don't know efficient way search looking in string , push proper array.
i tried x coordinates of images, start , end positions way off.
var ix1 = newparams.indexof("img"+(1)+"x"); var ix2 = (ix1 + 7); var res = newparams.substr(ix2,(ix2+2)); if send "res" console.log, get: 1","img1y:111","im result.
when put loop, , sub "1" var i=0, gets more wacky.
here jfiddle of attempt: http://jsfiddle.net/s9rgh/
split friend. see if did here helps: jsfiddle
you can use split segment string individual name/value pairs , put them in array easy access.
Comments
Post a Comment