繁体   English   中英

如何对键的值进行排序?

[英]How to sort values of keys?

从 api 我得到这个字符串:“增加 18% 的效果区域”,“增加 25% 的效果区域”,“增加 19% 的效果区域”

我想做这样的:“(18%-25%)增加效果区域”

 const filtredArray = array.filter((item) => item.implicit_mods);
 let implicitsList = [];

 filtredArray.forEach((item) => implicitsList.push(item.implicit_mods));

 implicitsList.forEach((implisit) => {
   if (implisit) {
     implisit.forEach((implicit) => {
       if (implicit) {
         if (!/\d/.test(implicit)) {
           //implicits without number
           if (!uniqueImplicitMap.get(implicit)) {
             addToMap(uniqueImplicitMap, implicit);
           }
         } else {
           // implicits with number
           let value = implicit.match(/\d+/g);
           let replaceImplicit = implicit.replace(/\d+/g, '#');
           if (value.length >= 2) {
             addToMap(uniqueImplicitMap, replaceImplicit, [
               value[0],
               value[1],
             ]);
           } else {
             addToMap(uniqueImplicitMap, replaceImplicit, value);
           }
         }
       }
     });
   }
 });
} 

function addToMap(map, key, value) {
 if (!map.get(key)) {
   if (value) {
     map.set(key, [value, value]);
   } else {
     map.set(key);
   }
 }
 if (map.get(key) && value) {
   if (value[0] > map.get(key)[0]) {
     map.get(key).splice(0, 1, value);
   } else if (value < map.get(key)[0] && value < map.get(key)[1]) {
     map.get(key).splice(1, 1, value);
   }
 }
}

如果我的字符串中只有一个数字,我可以做到这一点,但如果字符串是这样的:“10% 的机会在杀死时获得 3 秒的邪恶力量”

代码中断。

请帮帮我

我不完全理解你的代码。 我想你想要的是,如果你从 API 得到一个字符串数组,比如["18% increased Area of Effect", "25% increased Area of Effect", "19% increased Area of Effect"] ,你想使其变为(18%-25%) increased Area of Effect

如果是这样,您可以在数组上运行.map以查找带符号的百分比,并获得百分比。 这样就很容易使用 ES6 模板文字生成字符串。

示例代码

JS Fiddle 的片段。

 // Select elements on HTML const before = document.querySelector(".previous"), before2 = document.querySelector(".previous2"), after = document.querySelector(".after"), after2 = document.querySelector(".after2"); // Sample API text const beforeText = ["18% increased Area of Effect", "25% increased Area of Effect", "19% increased Area of Effect"]; const before2Text = ["10% chance to gain Unholy Might for 3 seconds on Kill", "18% chance to gain Unholy Might for 3 seconds on Kill", "26% chance to gain Unholy Might for 3 seconds on Kill"]; // Display before paresed strings before.innerText = beforeText.join(" -=- "); before2.innerText = before2Text.join(" -=- "); // Display parsed strings after.innerText = parser(beforeText); after2.innerText = parser(before2Text); function parser(arrText) { const PERCENT_REGEX = /[0-9]{1,2}%/; // Get percents of strings. const percents = arrText.map(elem => { const percentStr = elem.match(PERCENT_REGEX)[0]; return parseInt( // Remove percent symbol percentStr.substring(0, percentStr.length - 1), 10 ); }); const chanceToDoWhat = arrText[0].replace(PERCENT_REGEX, ""); return `(${Math.min(...percents)}%-${Math.max(...percents)}%)${chanceToDoWhat}`; }
 Before Parse:<br> <code class="previous"></code> <br> <br> Before Parse 2:<br> <code class="previous2"></code> <br> <br> After Parse:<br> <code class="after"></code> <br> <br> After Parse 2:<br> <code class="after2"></code>

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM