简体   繁体   English

在Javascript中解析复杂字符串的最有效方法

[英]Most efficient way to parse a complex string in Javascript

I'm having trouble parsing this 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 the height, width, x and y coordinates of both text objects and image objects that I need to position and resize in the DOM. 该字符串包含我需要在DOM中定位和调整大小的文本对象和图像对象的高度,宽度,x和y坐标。

The first 2 values in the array are the number of textfields, and the number of images (which i was going to use in loops as the max number of iterations). 数组中的前2个值是文本字段的数量,以及图像的数量(我将在循环中使用它作为最大迭代次数)。

I need to parse the number values, and 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), the number 51 would be pushed into the imgXcoords array at position 0 . 例如,在“ img1x:51 ”(图像1,Y坐标)中,数字51将被推入位置0处的imgXcoords数组中。

and in " img2y:111 " (image 2, Y coord), the number 111 would be pushed into the imgYcoords array at position 1 . 并且在“ img2y:111 ”(图像2,Y坐标)中,数字111将被推入位置1imgYcoords数组中。

and in " txt2w:303 " (textfield 2, width), the number 303 would be pushed into the txtBoxWidth array at position 1. 并且在“ txt2w:303 ”(文本字段2,宽度)中,数字303将被推入位置1的txtBoxWidth数组中。

If someone can show me just how to push the img(i)x and img(i)y values into their arrays (imgXcoords and imgYcoords) using a loop, I can figure out the rest from there. 如果有人可以告诉我如何使用循环将img(i)x和img(i)y值推入他们的数组(imgXcoords和imgYcoords),我可以从中找出其余部分。

I just don't know the most efficient way to search what I am looking for in the string and push it into the proper array. 我只是不知道在字符串中搜索我要查找的内容的最有效方法,并将其推入正确的数组。

I tried this for the x coordinates of the images, but my start and end positions are way off. 我尝试了这个图像的x坐标,但我的开始和结束位置都很偏僻。

var iX1 = newParams.indexOf("img"+(1)+"x");
var iX2 = (iX1 + 7);
var res = newParams.substr(iX2,(iX2+2));

if I send "res" to the console.log, I get: 1","img1y:111","im as a result. 如果我将“res”发送到console.log,我得到: 1","img1y:111","im的结果。

When I put that into a loop, and sub "1" with var i=0, it gets even more wacky. 当我把它放入一个循环中,并且变量i = 0的子“1”时,它变得更加古怪。

Here is a jfiddle of my attempt: http://jsfiddle.net/S9RGH/ 这是我尝试的一个方面: http//jsfiddle.net/S9RGH/

split is your friend. split是你的朋友。 See if what I did here helps: jsfiddle 看看我在这里做了什么有帮助: jsfiddle

You can use split to segment the string into individual name/value pairs and put them in an array for easy access. 您可以使用split将字符串分段为单个名称/值对,并将它们放在一个数组中以便于访问。

While this isn't what I could call the most efficient code, it is I believe the most readable solution... 虽然这不是我称之为最有效的代码,但我相信最可读的解决方案......

I'd only work about optimizing it if it turned out to be a major performance hit. 如果它被证明是一个重大的性能影响我只会努力优化它。 To optimize it, I'd go through the data object and turn it into a cleaner structure to parse. 为了优化它,我将浏览data对象并将其转换为更清晰的结构进行解析。

var data = [
  "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"
];

var imgXcoords = [];
var imgYcoords = [];
var imgHeight = [];
var imgWidth = [];

var bucket;

// Trimmed for simplicty
// The names of the fields we are looking for
var fields = ["img?x", "img?y", "img?h", "img?w"];

// The arrays they go in.
// Keep these in the same order as the 'fields' above.
var dests = [imgXcoords,
  imgYcoords,
  imgHeight,
  imgWidth
];


// Since the counts starts at '1'
var imageCount = +data[1] + 1;

// Go through the images
for (var i = 0; i < imageCount; i++) {

   // And each field we are looking for
  for (var ii = 0; ii < fields.length; ii++) {

    // Make the name of the thing we are looking for.
    var wantedKey = fields[ii];
    wantedKey = wantedKey.replace("?", i);

    // Then walk through the array. FUGLY
    // I know, but simple.
    for (var j = 0; j < data.length; j++) {

      // Split the value up.
      var arr = data[j].split(':');


      if (arr) {
        var currentKey = arr[0];
        // Force to a number
        var value = +arr[1];

        console.log([currentKey, wantedKey]);

         // we found what we want? Great!
        if (currentKey === wantedKey) {
          bucket = dests[ii];
          bucket.push(value);
          continue;
        }
      }
    }
  }
}

debugger;   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JavaScript集转换为字符串的最有效方法 - The most efficient way to convert a javascript set to string 在JavaScript中重组对象的复杂数组的最有效方法? - Most efficient way to restructure complex array of objects in javascript? 以最有效的方式提取字符串中包含在复杂括号中的字符 - Extract chars enclosed in complex brackets within a string in most efficient way 在 JavaScript 中解析 CSS 颜色的最有效方法是什么? - What is the most efficient way to parse a CSS color in JavaScript? Javascript:将字符串转换为整数然后再转换回字符串的最有效方法 - Javascript: Most Efficient way to convert string to integer then back to string 使用JavaScript评估字符串是否是回文符的最有效方法是什么? - What's the most efficient way to evaluate if a string is a palindrome using Javascript? 使用 javascript 呈现字符串模板的最有效方法 - Most efficient way for rendering string templates using javascript 在Java中通过字符串的最有效方式是什么? - What is the most efficient way to go through string's chars in Javascript? 返回包含字符串的嵌套数组的最有效方法 (JavaScript) - Most efficient way to return a nested array that includes a string (JavaScript) 比较两个字符串数组Javascript的最快/最有效的方法 - Fastest / most efficient way to compare two string arrays Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM