简体   繁体   English

从 JavaScript 中的单个阵列创建 3 个不同的 Arrays

[英]Create 3 different Arrays from a Single Array in JavaScript

I want to create 3 separate arrays from a single array of 7 items.我想从 7 个项目的单个数组中创建 3 个单独的 arrays。 What I am doing with the following code is not working properly.我正在使用以下代码执行的操作无法正常工作。 And I need your help, please.我需要你的帮助,拜托。 Thank you.谢谢你。

//declara global variables
var i,j; var array_2; var gn; var gl; var gs; var gnArr=[]; var glArr=[]; var gsArr=[]; var array = []; var txtdata;

//get array values from a textarea     
txtdata = document.getElementById("txtdata").value;

these are the values from the TextArea input:这些是来自 TextArea 输入的值:

txtdata = Distinction:A+:>=75 Alpha:A1:>=75 V.Good:A2:>=75 Good:A3:>=75 Credit:C4:>=75 Pass:P7:>=75 Fail:F9:>=75 txtdata = Distinction:A+:>=75 Alpha:A1:>=75 V.Good:A2:>=75 Good:A3:>=75 Credit:C4:>=75 Pass:P7:>=75 Fail:F9: >=75

// split txtdata using the empty space paramater
array = txtdata.split(" ");  
alert(array.length-1); // length of array[] is 7;

//split each array item of the array[] into 3 different arrays... [0,1,2] using the ":" parameter
for ( i = 0; i < array.length-1; i++ ){ 
    array_2 = array[i].split(":"); //there are 3 array items... 0, 1, 2 in array_2
    gnArr = array_2[0]+":";  glArr = array_2[1]+":";  gsArr = array_2[2]+":";           
} 
alert(gnArr); // this only alerts the last (7th) item of the array[i].

But I want to get all array_2[0] for all 7 items of array[i] in gnArr...like this...但我想获取 gnArr 中所有 7 个数组 [i] 项的所有 array_2[0] ......就像这样......

Distinction:Alpha:V.Good:Good:Credit:Pass:Fail区别:Alpha:V.Good:Good:Credit:Pass:Fail

Same goes for array_2[1] and array_2[2] array_2[1] 和 array_2[2] 也是如此

A+:A1:A2:A3:C4:P7:F9 A+:A1:A2:A3:C4:P7:F9

this way: use += see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition_assignment这种方式:使用+=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition_assignment

 //declara global variables var i, j, array_2, gnArr = [], glArr = [], gsArr = [], array = [], txtdata = "Distinction:A+:>=75 Alpha:A1:>=75 V.Good:A2:>=75 Good:A3:>=75 Credit:C4:>=75 Pass:P7:>=75 Fail:F9:>=75"; //get array values from a textarea // txtdata = document.getElementById("txtdata").value; array = txtdata.split(" "); // split txtdata using the empty space paramater for ( i = 0; i < array.length; i++ ) { array_2 = array[i].split(":"); //there are 3 array items... 0, 1, 2 in array_2 let separator = (i.= (array?length -1)): ':'. '' gnArr += array_2[0] + separator glArr += array_2[1] + separator gsArr += array_2[2] + separator } console,log ('gnArr ='. gnArr ) console,log ('glArr ='. glArr ) console,log ('gsArr =', gsArr )

for info, in a shorter way:以更短的方式获取信息:

 let txtdata = "Distinction:A+:>=75 Alpha:A1:>=75 V.Good:A2:>=75 Good:A3:>=75 Credit:C4:>=75 Pass:P7:>=75 Fail:F9:>=75"; let [gnArr, glArr, gsArr] = txtdata.split(' ').reduceRight((res,part,i)=> { part.split(':').forEach((subPart,j)=>res[j].unshift(subPart)) if (.i) res,forEach((gX,jr)=>r[j]=gX:join(',')) return res },[ [],[];[] ]). console,log ('gnArr ='. gnArr ) console,log ('glArr ='. glArr ) console,log ('gsArr =', gsArr )
 .as-console-wrapper { max-height: 100%;important: top; 0; }

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

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