字符串拆分并计算字符串的总和与数字

时间:2016-10-13 09:56:35

标签: jquery object text split counter

我正在尝试对这个长字符串进行字符串拆分并将所有出现次数与相同字符串拆分的x之前的数字相加

<div id="all">3xOrange;2xBlue;1xRed;1xRed;1xRed;1xOrange;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xOrange;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;20xBlue;33xRed;20xBlue;33xRed;2xBlue;3xRed;51xBlue;51xRed;</div>

<b id="total"></b>

我正在努力解决这个问题:

var obj = {};

$("#all").text().split(";").forEach(function(text){
  if (text != ""){
    var part = text.split("x");
    obj[part[1]] = obj[part[1]] != undefined ? obj[part[1]]+1 : parseInt(part[0]);
  }
});
for (key in obj) {
  $("#total").append(obj[key] +"×"+ key + "; ");
}

问题在于它虽然理论上有效,但是当字符串太长时,就像上面发布的字符串一样,一些字符串拆分会被求和忽略。 Orange Red and Blue的总和不正确。

Codepen code here!

请帮助,我如何修复此功能以正确地对我的字符串拆分进行总结。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

首先split ; x,然后在Object.keys(array).forEach(function(key, index) {},array);进行第二次分割,并在总数中添加第一行。

创建一个关联数组,用于保存每个键的总数。并使用for(var key in array){}方法或var array = $("#all").text().split(';'); var total=0; var colorarray = new Array(); $.each(array,function(key,value){ if(value!='') { if(colorarray[value.split("x")[1]]==undefined) { colorarray[value.split("x")[1]]=0; } var itemnumber = parseInt(value.split("x")[0]); colorarray[value.split("x")[1]]+=(itemnumber); total+=(itemnumber); } }); for(var key in colorarray) { $("#total").append("<div>"+key+" : "+colorarray[key]+"</div>"); }; $("#total").append("<div>total :"+total+"</div>");

在此数组中循环

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="all">3xOrange;2xBlue;1xRed;1xRed;1xRed;1xOrange;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xOrange;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;20xBlue;33xRed;20xBlue;33xRed;2xBlue;3xRed;51xBlue;51xRed;</div>

<b id="total"></b>
&#13;
java.lang.NoClassDefFoundError: com/android/utils/ILogger
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.android.utils.ILogger
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more
Error: A JNI error has occurred, please check your installation and try again
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=350m; support was removed in 8.0
Exception in thread "main" 
&#13;
&#13;
&#13;