如何在JavaScript中基于多个分隔符将字符串拆分为多个数组

huangapple go评论102阅读模式
英文:

How to split a string to multiple arrays based on multiple separators in javascript

问题

I have seen the questions but none of them helped me.
I have a string like this:

  1. var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";

I want that string to be in different arrays based on the separators "HU", "EN", "DE", "RO".

My approach currently is this (Working but not too elegant):

  1. var Lang_Array1 = Lang_Array.split(",");
  2. console.log(typeof(Lang_Array));
  3. console.log(Lang_Array);
  4. var HU_Langs = [];
  5. var EN_Langs = [];
  6. var DE_Langs = [];
  7. var RO_Langs = [];
  8. for (var i = 0; i < Lang_Array1.length; i++) {
  9. if (Lang_Array1[i] != "EN") {
  10. if (Lang_Array1[i] != "") {
  11. HU_Langs[i] = Lang_Array1[i];
  12. }
  13. } else {
  14. for (i; i < Lang_Array1.length; i++) {
  15. if (Lang_Array1[i] != "DE") {
  16. if (Lang_Array1[i] != "") {
  17. EN_Langs[i] = Lang_Array1[i];
  18. }
  19. } else {
  20. for (i; i < Lang_Array1.length; i++) {
  21. if (Lang_Array1[i] != "RO") {
  22. if (Lang_Array1[i] != "") {
  23. DE_Langs[i] = Lang_Array1[i];
  24. }
  25. } else {
  26. for (i; i < Lang_Array1.length; i++) {
  27. if (Lang_Array1[i] != "") {
  28. RO_Langs[i] = Lang_Array1[i];
  29. }
  30. }
  31. break;
  32. }
  33. }
  34. break;
  35. }
  36. }
  37. break;
  38. }
  39. }

That way I get what I want, but I want to improve it somehow.

The arrays:

  1. HU_Langs = ["HU", "blah", "blah", "blah"];
  2. EN_Langs = ["EN", "blah", "blah", "blah"];
  3. DE_Langs = ["DE", "blah", "blah", "blah"];

etc...

So how can I improve this code without nested for loops?

EDIT: Thank you for all! All the answers are very, very good.
My question wasn't clear and detailed enough, but I solved it like this with the help of the correct answer.

Here is the function now:

  1. function Get_Language_Object(Lang_Array){
  2. Lang_Array = Lang_Array.replace(/(\r\n|\n|\r)/gm, "");
  3. var langs = ['HU', 'EN', 'DE', 'RO'];
  4. var isLang = str => langs.includes(str);
  5. var { HU: New_HU_Langs, EN: New_EN_Langs, DE: New_DE_Langs, RO: New_RO_Langs } = Lang_Array.split(',')
  6. .reduce((r, str) => {
  7. if (isLang(str)) r.push([]);
  8. r[r.length - 1].push(str);
  9. return r;
  10. }, [])
  11. .reduce((r, [code, ...arr]) => ({ ...r, [code]: arr }), {});
  12. for (var i = 0; i < TAGS.length; i++) {
  13. arrLang.HU[TAGS[i]] = New_HU_Langs[i];
  14. arrLang.EN[TAGS[i]] = New_EN_Langs[i];
  15. arrLang.DE[TAGS[i]] = New_DE_Langs[i];
  16. arrLang.RO[TAGS[i]] = New_RO_Langs[i];
  17. }
  18. Set_Actual_Language();
  19. VNotify("Settings Notify", "Lang Set!", "success", 1500, "success32.png");
  20. }
英文:

I have seen the questions but none of them helped me.
I have a string like this:

var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";

I want that string to be in different arrays based on the separators "HU", "EN", "DE", "RO".

My approach currently is this(Working but not too elegant):

  1. var Lang_Array1 = Lang_Array.split(&quot;,&quot;);
  2. console.log(typeof(Lang_Array));
  3. console.log(Lang_Array);
  4. var HU_Langs = [];
  5. var EN_Langs = [];
  6. var DE_Langs = [];
  7. var RO_Langs = [];
  8. for(var i = 0; i &lt; Lang_Array1.length;i++){
  9. if(Lang_Array1[i] != &quot;EN&quot;){
  10. if(Lang_Array1[i] != &quot;&quot;){
  11. HU_Langs[i] = Lang_Array1[i];
  12. }
  13. }else{
  14. for(i;i &lt; Lang_Array1.length;i++){
  15. if(Lang_Array1[i] != &quot;DE&quot;){
  16. if(Lang_Array1[i] != &quot;&quot;){
  17. EN_Langs[i] = Lang_Array1[i];
  18. }
  19. }else{
  20. for(i;i &lt; Lang_Array1.length;i++){
  21. if(Lang_Array1[i] != &quot;RO&quot;){
  22. if(Lang_Array1[i] != &quot;&quot;){
  23. DE_Langs[i] = Lang_Array1[i];
  24. }
  25. }else{
  26. for(i;i &lt; Lang_Array1.length;i++){
  27. if(Lang_Array1[i] != &quot;&quot;){
  28. RO_Langs[i] = Lang_Array1[i];
  29. }
  30. }
  31. break;
  32. }
  33. }
  34. break;
  35. }
  36. }
  37. break;
  38. }
  39. }

That way i get what i want but i want to improve it somehow.
The arrays:

  1. HU_Langs =[&quot;HU&quot;,&quot;blah&quot;,&quot;blah&quot;,&quot;blah&quot;];
  2. EN_Langs =[&quot;EN&quot;,&quot;blah&quot;,&quot;blah&quot;,&quot;blah&quot;];
  3. DE_Langs =[&quot;DE&quot;,&quot;blah&quot;,&quot;blah&quot;,&quot;blah&quot;];

etc...

So how can i improve this code without nested for loops?

EDIT: Thank you for all! All the answers are very very good.
My question wasnt clear and detailed enough but i solved it like this with the help of the correct answer.

Here is the function now:

  1. function Get_Language_Object(Lang_Array){
  2. Lang_Array = Lang_Array.replace(/(\r\n|\n|\r)/gm, &quot;&quot;);
  3. var langs = [&#39;HU&#39;, &#39;EN&#39;, &#39;DE&#39;, &#39;RO&#39;];
  4. var isLang = str =&gt; langs.includes(str);
  5. var { HU: New_HU_Langs, EN: New_EN_Langs, DE: New_DE_Langs, RO: New_RO_Langs } = Lang_Array.split(&#39;,&#39;)
  6. .reduce((r, str) =&gt; {
  7. if(isLang(str)) r.push([]);
  8. r[r.length - 1].push(str);
  9. return r;
  10. }, [])
  11. .reduce((r, [code, ...arr]) =&gt; ({ ...r, [code]: arr }), {});
  12. for(var i = 0; i &lt; TAGS.length;i++){
  13. arrLang.HU[TAGS[i]] = New_HU_Langs[i];
  14. arrLang.EN[TAGS[i]] = New_EN_Langs[i];
  15. arrLang.DE[TAGS[i]] = New_DE_Langs[i];
  16. arrLang.RO[TAGS[i]] = New_RO_Langs[i];
  17. }
  18. Set_Actual_Language();
  19. VNotify(&quot;Settings Notfy&quot;,&quot;Lang Set!&quot;,&quot;success&quot;,1500,&quot;success32.png&quot;);
  20. }

答案1

得分: 3

按逗号(,)分割字符串,减少数组,并为每个语言代码添加一个新的子数组。将所有项目推送到最后一个子数组中:

  1. var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
  2. var langs = ['HU', 'EN', 'DE', 'RO'];
  3. var isLang = str => langs.includes(str);
  4. var result = Lang_Array1.split(',')
  5. .reduce((r, str) => {
  6. if (isLang(str)) r.push([]);
  7. r[r.length - 1].push(str);
  8. return r;
  9. }, []);
  10. console.log(result);

如果你想要将其拆分成多个数组,将子数组减少为对象,并使用解构分配给变量:

  1. var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
  2. var langs = ['HU', 'EN', 'DE', 'RO'];
  3. var isLang = str => langs.includes(str);
  4. var { HU: HU_Langs, EN: EN_Langs, DE: DE_langs, RO: RO_langs } = Lang_Array1.split(',')
  5. .reduce((r, str) => {
  6. if (isLang(str)) r.push([]);
  7. r[r.length - 1].push(str);
  8. return r;
  9. }, [])
  10. .reduce((r, [code, ...arr]) => ({ ...r, [code]: arr }), {});
  11. console.log(HU_Langs, EN_Langs, DE_langs, RO_langs);
英文:

Split the string by the delimiter (,), reduce the array, and for every language code, add a new sub-array. Push all items to the last sub-array:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var Lang_Array1 = &quot;HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah&quot;;
  2. var langs = [&#39;HU&#39;, &#39;EN&#39;, &#39;DE&#39;, &#39;RO&#39;];
  3. var isLang = str =&gt; langs.includes(str);
  4. var result = Lang_Array1.split(&#39;,&#39;)
  5. .reduce((r, str) =&gt; {
  6. if(isLang(str)) r.push([]);
  7. r[r.length - 1].push(str);
  8. return r;
  9. }, []);
  10. console.log(result);

<!-- end snippet -->

If you want to split to multiple arrays, reduce the sub-arrays to an object, and use desturcturing to assign them to variables:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var Lang_Array1 = &quot;HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah&quot;;
  2. var langs = [&#39;HU&#39;, &#39;EN&#39;, &#39;DE&#39;, &#39;RO&#39;];
  3. var isLang = str =&gt; langs.includes(str);
  4. var { HU: HU_Langs, EN: EN_Langs, DE: DE_langs, RO: RO_langs } = Lang_Array1.split(&#39;,&#39;)
  5. .reduce((r, str) =&gt; {
  6. if(isLang(str)) r.push([]);
  7. r[r.length - 1].push(str);
  8. return r;
  9. }, [])
  10. .reduce((r, [code, ...arr]) =&gt; ({ ...r, [code]: arr }), {});
  11. console.log(HU_Langs, EN_Langs, DE_langs, RO_langs);

<!-- end snippet -->

答案2

得分: 0

你应该在分割后减少单词列表,以逗号(,)为分隔符。每当遇到已知的关键字时,你会修改指向当前语言的 key

这是最简洁的示例:

  1. let arr = "HU,blaf,blaf,blaf,EN,blah,blah,blah,blah,DE,bla,bla,bla,RO,bah,bah,bah"
  2. let keys = [ "DE", "EN", "HU", "RO" ]
  3. let dict = langDict(arr, keys)
  4. console.log(dict)
  5. function langDict(arr, keys) {
  6. let key = null
  7. return arr.split(/,/g).reduce((dict, token) => {
  8. if (Object.keys(dict).length && key == null) {
  9. throw new Error('No language defined yet!')
  10. } else if (keys.includes(token)) {
  11. key = token
  12. } else {
  13. if (dict[key] == null) dict[key] = []
  14. dict[key].push(token)
  15. }
  16. return dict
  17. }, {})
  18. }
  1. .as-console-wrapper { top: 0; max-height: 100% !important; }
英文:

You should reduce the list of words after splitting on your delimiter (,). Each time you run into a known key, you alter the key that refers to the current language.

This is the most succinct example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let arr = &quot;HU,blaf,blaf,blaf,EN,blah,blah,blah,blah,DE,bla,bla,bla,RO,bah,bah,bah&quot;
  2. let keys = [ &quot;DE&quot;, &quot;EN&quot;, &quot;HU&quot;, &quot;RO&quot; ]
  3. let dict = langDict(arr, keys)
  4. console.log(dict)
  5. function langDict(arr, keys) {
  6. let key = null
  7. return arr.split(/,/g).reduce((dict, token) =&gt; {
  8. if (Object.keys(dict).length &amp;&amp; key == null) {
  9. throw new Error(&#39;No language defined yet!&#39;)
  10. } else if (keys.includes(token)) {
  11. key = token
  12. } else {
  13. if (dict[key] == null) dict[key] = []
  14. dict[key].push(token)
  15. }
  16. return dict
  17. }, {})
  18. }

<!-- language: lang-css -->

  1. .as-console-wrapper { top: 0; max-height: 100% !important; }

<!-- end snippet -->

答案3

得分: 0

如我认为大写的ISO2代码是语言提示,您可以以更具可扩展性的方式将所有结果分组在一起。

  1. var Lang_Array1 = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
  2. var Languages = {};
  3. var current = '';
  4. Lang_Array1.split(',').forEach(function (value) {
  5. if (/^[A-Z]{2}$/.test(value))
  6. current = value;
  7. else
  8. Languages[current] = (Languages[current] || []).concat(value);
  9. });
  10. console.log(Languages);

上面的代码片段会将Languages对象填充如下:

  1. {
  2. "HU": [
  3. "blah",
  4. "blah",
  5. "blah"
  6. ],
  7. "EN": [
  8. "blah",
  9. "blah",
  10. "blah",
  11. "blah"
  12. ],
  13. "DE": [
  14. "blah",
  15. "blah",
  16. "blah"
  17. ],
  18. "RO": [
  19. "blah",
  20. "blah",
  21. "blah"
  22. ]
  23. }

此时,您只需直接访问Languages.EN或其他语言,或通过以下方式重新创建原始结构:

  1. var Lang_Array = Object.keys(Languages).reduce(
  2. function (arr, key) {
  3. return arr.concat(key, Languages[key]);
  4. },
  5. []
  6. );

您还可以创建以语言从索引0开始,后跟值的数组:

  1. var Lang_EN = ['EN'].concat(Languages.EN);

总之,按键分组似乎是表示和操作这种扁平结构的最佳方法。

希望这有所帮助 👋

英文:

As I believe the upper case ISO2 code is the language hint, you could group all results together in a more scalable way.

  1. var Lang_Array1 = &quot;HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah&quot;;
  2. var Languages = {};
  3. var current = &#39;&#39;;
  4. Lang_Array1.split(&#39;,&#39;).forEach(function (value) {
  5. if (/^[A-Z]{2}$/.test(value))
  6. current = value;
  7. else
  8. Languages[current] = (Languages[current] || []).concat(value);
  9. });
  10. console.log(Languages);

Above snippet would populate the Languages object like this:

  1. {
  2. &quot;HU&quot;: [
  3. &quot;blah&quot;,
  4. &quot;blah&quot;,
  5. &quot;blah&quot;
  6. ],
  7. &quot;EN&quot;: [
  8. &quot;blah&quot;,
  9. &quot;blah&quot;,
  10. &quot;blah&quot;,
  11. &quot;blah&quot;
  12. ],
  13. &quot;DE&quot;: [
  14. &quot;blah&quot;,
  15. &quot;blah&quot;,
  16. &quot;blah&quot;
  17. ],
  18. &quot;RO&quot;: [
  19. &quot;blah&quot;,
  20. &quot;blah&quot;,
  21. &quot;blah&quot;
  22. ]
  23. }

At this point, all you have to do is to address directly Languages.EN or others, or recreate the original structure via:

  1. var Lang_Array = Object.keys(Languages).reduce(
  2. function (arr, key) {
  3. return arr.concat(key, Languages[key]);
  4. },
  5. []
  6. );

You could also create arrays with the language starting at index 0, and values following:

  1. var Lang_EN = [&#39;EN&#39;].concat(Languages.EN);

As summary, grouping by keys seem the best way to represent, and manipulate, such flattened structure.

I hope this helped 👋

答案4

得分: 0

Response:

  1. {hu: ["blah1", "blah2", "blah3"], en: ["blah4", "blah5", "blah6", "blah7"], de: ["blah8", "blah9", "blah10"], ro: ["blah11", "blah12", "blah13"]}
英文:

If the order is constant:

  1. var Lang_Array1 = &quot;HU,blah1,blah2,blah3,EN,blah4,blah5,blah6,blah7,DE,blah8,blah9,blah10,RO,blah11,blah12,blah13&quot;;
  2. const arr1 = Lang_Array1.split(/[HU,EN,DE,RO]/g);
  3. let obj = {hu:[],en:[],de:[],ro:[]};
  4. const keys = Object.keys(obj);
  5. let i = 0;
  6. arr1.forEach(el=&gt;{
  7. if(el !== &#39;&#39;)
  8. obj[ keys[i] ].push(el);
  9. else
  10. if(obj[ keys[i] ].length &gt; 0)
  11. i++;
  12. });
  13. console.log(obj);

Response:

  1. {hu: Array(3), en: Array(4), de: Array(3), ro: Array(3)}
  2. hu: (3) [&quot;blah1&quot;, &quot;blah2&quot;, &quot;blah3&quot;]
  3. en: (4) [&quot;blah4&quot;, &quot;blah5&quot;, &quot;blah6&quot;, &quot;blah7&quot;]
  4. de: (3) [&quot;blah8&quot;, &quot;blah9&quot;, &quot;blah10&quot;]
  5. ro: (3) [&quot;blah11&quot;, &quot;blah12&quot;, &quot;blah13&quot;]

答案5

得分: 0

临时数组可用于引用要推送到数组的内容:

  1. var Lang_Array = "HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah";
  2. var Lang_Array1 = Lang_Array.split(","), HU_Langs = [], EN_Langs = [], DE_Langs = [], RO_Langs = [];
  3. for (var Langs = [], i = 0; i < Lang_Array1.length; i++)
  4. {
  5. var str = Lang_Array1[i];
  6. Langs = { HU: HU_Langs, EN: EN_Langs, DE: DE_Langs, RO: RO_Langs }[str] || Langs;
  7. Langs.push(str);
  8. }
  9. console.log( HU_Langs, EN_Langs, DE_Langs, RO_Langs );
英文:

Temporary array can be used to reference the array to push to :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. var Lang_Array = &quot;HU,blah,blah,blah,EN,blah,blah,blah,blah,DE,blah,blah,blah,RO,blah,blah,blah&quot;;
  2. var Lang_Array1 = Lang_Array.split(&quot;,&quot;), HU_Langs = [], EN_Langs = [], DE_Langs = [], RO_Langs = [];
  3. for (var Langs = [], i = 0; i &lt; Lang_Array1.length; i++)
  4. {
  5. var str = Lang_Array1[i];
  6. Langs = { HU: HU_Langs, EN: EN_Langs, DE: DE_Langs, RO: RO_Langs }[str] || Langs;
  7. Langs.push(str);
  8. }
  9. console.log( HU_Langs, EN_Langs, DE_Langs, RO_Langs );

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 20:39:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定